jsonjolt

How to dynamically add key and values from one object into another object in an array via jolt


I'm using jolt and I have an input object where I would like to take the keys out of one property and insert them into each object of an array in another property dynamically:

My input:

{
  "data": {
    "NAN_KEY": 1,
    "TEMP": 3
  },
  "attributes": [
    {
      "name": "attribute1",
      "value": 3
    },
    {
      "name": "attribute2",
      "value": 2
    }
  ]
}

The result I'm aiming for:

 "attributes": [
    {
      "name": "attribute1",
      "value": 3,
      "NAN_KEY": 1,
      "TEMP": 3
    },
    {
      "name": "attribute2",
      "value": 2,
      "NAN_KEY": 1,
      "TEMP": 3
    }
  ]

This question was posted previously in this thread

But after using the solution, I realized I needed it to dynamically add the entire object instead of hardcoding the fields

Any help is appreciated!


Solution

  • Yes, it's possible to make it more dynamic such as

    [
      {
        "operation": "shift",
        "spec": {
          "attributes": {
            "*": {
              "@2,data": { "*": "[&1].&" }, // go two levels up the tree to grab the values of the "data" object
              "*": "[&1].&"
            }
          }
        }
      }
    ]
    

    the demo on the site http://jolt-demo.appspot.com/ is

    enter image description here