Search code examples
jsontransformjolt

Convert a list of list to list of Maps with a value


I am trying to convert the folowing input to the desired output using Jolt

[
  {
    "scores": [
      [
        4,
        1
      ],
      [
        2,
        1
      ],
      [
        8,
        5
      ],
      [
        7,
        3
      ],
      [
        5,
        10
      ]
    ]
  }
]

Desired Output

[
  {
    "value1": 4,
    "value2": 1
  },
  {
    "value1": 2,
    "value2": 1
  },
  {
    "value1": 8,
    "value2": 5
  },
  {
    "value1": 7,
    "value2": 3
  },
  {
    "value1": 5,
    "value2": 10
  }
]

I want to convert the list of list to the map. The elements in the input list should be the values of the keys.

Can anyone help with the Jolt Spec.

Thanks in advance.


Solution

  • You can use the following shift transformation spec which incurs indexes 0 and 1 respectively as looping through in the scores array it such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "scores": {          
              "*": {
                "0": "[&1].value1", // the first component of the subarray
                "1": "[&1].value2"  // the second component of the subarray
              }
            }
          }
        }
      }
    ]
    

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

    enter image description here