Search code examples
javajsonjolt

Jolt - Array into Key Value Pairs


I am trying to convert this input below to the expected output using Jolt

Here's my Input:

[
  [
    "station_name",
    "station_longname"
  ],
  [
    "10 Mile Brook Dam Water Level Daily Value, South W",
    "10 Mile Brook Dam Water Level Daily Value, South West Region"
  ]
]

I am trying to convert it into this output -

{
  "station_name": "10 Mile Brook Dam Water Level Daily Value, South W",
  "station_longname": "10 Mile Brook Dam Water Level Daily Value, South West Region"
}

I tried using this Jolt -

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

But it came back like this -

[
  [
    "station_name",
    "10 Mile Brook Dam Water Level Daily Value, South W"
  ],
  [
    "station_longname",
    "10 Mile Brook Dam Water Level Daily Value, South West Region"
  ]
]

Solution

  • You can use the following shift transformation specs such as

    [
      { // dissipate each component of the respective arrays
        // to their respective object
        "operation": "shift",
        "spec": {
          "*": {
            "*": "&.comp&1"
          }
        }
      },
      { // match components within each object
        "operation": "shift",
        "spec": {
          "*": {
            "@comp1": "@comp0"
          }
        }
      }
    ]
    

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

    enter image description here