bazaarvoice / jolt

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

Jolt - Merge two arrays and rename fields #1067

Open olka-fasolka opened 3 years ago

olka-fasolka commented 3 years ago

I have a problem writing objects to an array. Basically I want to merge the arrays and rename the fields, while keeping the objects seperately.

My input json looks like this:

{
   "board":[
      {
         "role":"Head of board", 
         "id":"111",
         "name":"John Snow"
      }
   ],
   "leaders":[
      {
         "role":"Accounting leader",
         "id":"222",
         "name":"Amanda Johns"
      },
      {
         "role":"HR leader",
         "id":"333",
         "name":"Frank Smith"
      }
   ]
}

This is my spec: (I am aware that the values in brackets are probably not right)

[
   {
      "operation":"shift",
      "spec":{
         "board":{
            "*":{
               "id":"employees.bosses[#2].emp_num",
               "role":"employees.bosses[#2].position",
               "name":"employees.bosses[#2].name"
            }
         },
         "leaders":{
            "*":{
               "id":"employees.bosses[#2].emp_num",
               "role":"employees.bosses[#2].position",
               "name":"employees.bosses[#2].name"
            }
         }
      }
   }
]

and this is my output:

{
    "employees": {
        "bosses": [ {
            "emp_num": ["111", "222"],
            "position": ["Head of board", "Accounting leader"],
            "name": ["John Snow", "Amanda Johns"]
        }, {
            "emp_num": "333",
            "position": "HR leader",
            "name": "Frank Smith"
        } ]
    }
}

while I expect output that looks like this:

{
    "employees": {
        "bosses": [ {
            "emp_num": "111",
            "position": "Head of board",
            "name": "John Snow"
        }, {
            "emp_num": "222",
            "position": "Accounting leader",
            "name": "Amanda Johns"
        }, {
            "emp_num": "333",
            "position": "HR leader",
            "name": "Frank Smith"
        } ]
    }
}

I have major troubles understanding what to do and how the [#n] work, I would really appreciate any help with fixing my spec and explaination why this does/does not work!

loveall commented 3 years ago

Try this :

[
  {
    "operation": "shift",
    "spec": {
      "board": "employees.bosses",
      "leaders": {
        "*": "employees.bosses"
      }
    }
   }
]
kunal15112001 commented 1 year ago

I think this is an appropriate solution for this input,


[
  {
    "operation": "shift",
    "spec": {
      "board": {
        "*": {
          "id": "employees.@(1,id).emp_num",
          "role": "employees.@(1,id).position",
          "*": "employees.@(1,id).&"
        }
      },
      "leaders": {
        "*": {
          "id": "employees.@(1,id).emp_num",
          "role": "employees.@(1,id).position",
          "*": "employees.@(1,id).&"
        }
      }
    }
   },
  {
    "operation": "shift",
    "spec": {
      "employees": {
        "*": {
          "@": "employees.bosses"
        }
      }
    }
  }
]