bazaarvoice / jolt

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

Look up of a key to value #1164

Open brcm8976 opened 1 year ago

brcm8976 commented 1 year ago

hi, i want to do a lookup of a key to a value ,Appreciate your help.

Input: { "itemList": [ { "item": "1", "name": "apple" }, { "item": "2", "name": "pear" }, { "item": "3", "name": "orange" } ], "itemHistory": [ { "apple": [ "2.0", "3.0" ], "orange": [ "2.4", "3.6" ] } ] }

expected output: { "itemList": [ { "item": "1", "name": "apple", "itemHistory":[ "2.0", "3.0" ] }, { "item": "2", "name": "pear" }, { "item": "3", "name": "orange", "itemHistory":[ "2.4", "3.6" ] } ]

}

DhruvSingh861 commented 1 year ago

I think this will help :)

spec:-

[
  {
    "operation": "shift",
    "spec": {
      "itemList": {
        "*": {
          "*": "itemList[#2].&",
          "@(0,name)": {
            "*": {
              "@(4,itemHistory[0].&)": "itemList[#4].itemHistory"
            }
          }
        }
      }
    }
  }
]

output:-

{
  "itemList" : [ {
    "itemHistory" : [ "2.0", "3.0" ],
    "item" : "1",
    "name" : "apple"
  }, {
    "item" : "2",
    "name" : "pear"
  }, {
    "itemHistory" : [ "2.4", "3.6" ],
    "item" : "3",
    "name" : "orange"
  } ]
}