Use Jolt transformation to seperate an array into multiple objects

44 Views Asked by At

I have a JSON input like this:

Input:

{
  "data": {
    "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",
    "value": "1"
  },
  {
    "indicator": "abc",
    "value": "2"
  },
  {
    "indicator": "abc",
    "value": "3"
  },
  {
    "indicator": "abc",
    "value": "4"
  },
  {
    "indicator": "abc",
    "value": "5"
  },
  {
    "indicator": "def",
    "value": "6"
  },
  {
    "indicator": "def",
    "value": "7"
  },
  {
    "indicator": "def",
    "value": "8"
  },
  {
    "indicator": "def",
    "value": "9"
  },
  {
    "indicator": "def",
    "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. Thank you very much.

I have tried writting a JOLT Transformation spec but I do not know how to do it. I hope someone could help me write one.

1

There are 1 best solutions below

0
On BEST ANSWER

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", // get value of the identifier after going 2 levels up the tree
              "@": "&3_&.&2" // &3 represents going 3 levels up to grab the values 
                             // of the indexes of the "data" array
                             // the others use the similar logic
            }
          }
        }
      }
    }
  },
  { // get rid of the object keys
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

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

enter image description here