Search code examples
arraysjsontransformjolt

How to transform array into desired output using jolt


I am using jolt to transform json array and stuck for a long while. The source is:

[
  {
    "id": 1,
    "name": "Will"
  },
  {
    "id": 2,
    "name": "Musk"
  },
  ...
]

and the expected output is:

[
  {
    "json": {
      "id": 1,
      "name": "Will"
    }
  },
  {
    "json": {
      "id": 2,
      "name": "Musk"
    }
  },
  ...
]

How should I write the jolt spec?


Solution

  • You should separate the objects by [&1] in order to distinguish by the indexes of the outermost array in an arraywise manner while that's followed by literal json to get it as the constant keys of the objects such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": "[&1].json.&"
          }
        }
      }
    ]
    

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

    enter image description here