JOLT array transformation: add key in all objects in list

Viewed 462

I have an input object:

{
  "array": [
    {
      "id": 1
    },
    {
      "id": 2
    }
  ],
  "object": {
    "fixed-value": "some-value"
  }
}

And I want to transform it into:

{
  "NewObject" : [ {
    "objectId" : 1,
    "fixedValue": "some-value"
  }, {
    "objectId" : 2,
    "fixedValue": "some-value"
  } ]
}

I made this JOLT spec which shifts the list of objects in array, but I'm not able to add the fixed-value key in all of these objects:

[
  {
    "operation": "shift",
    "spec": {
      "array": {
        "*": {
          "id": "NewObject[&1].objectId"
        }
      }
    }
  }
]
1 Answers

Check this spec,

Traverse back to the root and then select the fixed-value,

"@(2,object.fixed-value)": "NewObject[&1].fixedValue"

[
  {
    "operation": "shift",
    "spec": {
      "array": {
        "*": {
          "id": "NewObject[&1].objectId",
          "@(2,object.fixed-value)": "NewObject[&1].fixedValue"
        }
      }
    }
  }
]
Related