Use Jolt transformation to seperate an array into multiple objects and update an array into another array

I have a JSON input like this:

Input:

{
  "data": {
    "attribute": [
      {
        "name": "Name_1",
        "code": "TT01"
      },
      {
        "name": "Name_2",
        "code": "TT02"
      },
      {
        "name": "Name_3",
        "code": "TT03"
      },
      {
        "name": "Name_4",
        "code": "TT04-1"
      },
      {
        "name": "Name_5",
        "code": "TT04-2"
      }
    ],
    "data": [
      {
        "indicator": "abc",
        "value": [
          "1",
          "2",
          "3",
          "4",
          "5"
        ]
      },
      {
        "indicator": "def",
        "value": [
          "6",
          "7",
          "8",
          "9",
          "10"
        ]
      }
    ]
  }
}

I want a JSON output like this:

Output:

[
    { "indicator": "abc", "name": "Name_1", "value": "1" },
    { "indicator": "abc", "name": "Name_2",  "value": "2" },
    { "indicator": "abc", "name": "Name_3",  "value": "3" },
    { "indicator": "abc", "name": "Name_4",  "value": "4" },
    { "indicator": "abc", "name": "Name_5",  "value": "5" },
    { "indicator": "def", "name": "Name_1",  "value": "6" },
    { "indicator": "def", "name": "Name_2",  "value": "7" },
    { "indicator": "def", "name": "Name_3",  "value": "8" },
    { "indicator": "def", "name": "Name_4",  "value": "9" },
    { "indicator": "def", "name": "Name_5",  "value": "10" }
]

Does anyone know how to write a JOLT Transformation spec to get this output?

I have tried but I cannot figure out how to solve this.

I hope that someone could help me write one. Thank you very much.

>Solution :

You can use the following transformation containing shift operations :

[
  { // put the attributes into objects separatedly
    "operation": "shift",
    "spec": {
      "data": {
        "data": {
          "*": {
            "value": {
              "*": {
                "@2,indicator": "&3_&.indicator",
                "@4,attribute[&].name": "&3_&.name", // & in [&] uses the index values of "value" array
                "@": "&3_&.&2"
              }
            }
          }
        }
      }
    }
  },
  { // get rid of the object keys
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

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

enter image description here

Leave a Reply