bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.56k stars 329 forks source link

Convert string array into individual objects. #1212

Closed praveenvsl closed 1 year ago

praveenvsl commented 1 year ago

I'm trying to convert an array of strings into objects. Which is not workings as I expected. Can someone help me on this

Input:

GroupMembers: {
       group1: [ member1, member2, member3],
       group2: [ member1, member4, member6, member8],
       group3: [ member2, member6]
}

Expected output:

GroupMembers: [
     {
           groupId: group1,
           memberId: member1 
      },
     {
           groupId: group1,
           memberId: member2 
      },
     {
           groupId: group1,
           memberId: member3 
      },
     {
           groupId: group2,
           memberId: member1 
      },
 .....
.......
     {
           groupId: group3,
           memberId: member6 
      }
]

My Spec:

[
  {
    "operation": "shift",
    "spec": {
      "group": {
        "*": {
          "*": {
            "@(1,[&])": {
              "*": {
                "$(3)": "GroupMembers[#2].groupId",
                "$": "GroupMembers[#2].memberId"
              }
            }
          }
        }
      }
    }
    }
]
praveenvsl commented 1 year ago
[
  { // form objects from array keys vs. array components
    "operation": "shift",
    "spec": {
      "GroupMembers": {
        "*": {
          "*": {
            "$1": "&3.&2.&1.groupId", // partition by top level object, array and its indexes
            "@": "&3.&2.&1.memberId"
          }
        }
      }
    }
  },
  { // collect each key-value pairs under the common array node, namely "GroupMembers"
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": "&2[]"
        }
      }
    }
  }
]