Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading