DataD
DataD

Reputation: 65

How can I combine two arrays to create a key value pair with Jolt?

I've already created a spec to convert my JSON input

{
  "rows": [
    {
      "row": [
        "row1",
        "row2",
        "row3"
      ],
      "header": [
        "header1",
        "header2",
        "header3"
      ]
    },
    {
      "row": [
        "row4",
        "row5",
        "row6"
      ],
      "header": [
        "header4",
        "header5",
        "header6"
      ]
    }
  ]
}

to convert to key-value pairs as following object result :

{
  "header1" : "row1",
  "header2" : "row2",
  "header3" : "row3",
  "header4" : "row4",
  "header5" : "row5",
  "header6" : "row6"
}

Is this possible to do using Jolt?

Upvotes: 1

Views: 341

Answers (2)

Barbaros Özhan
Barbaros Özhan

Reputation: 65408

One option is to use the following shift transformation spec :

[
  {
    "operation": "shift",
    "spec": {
      "*s": { // rows array
        "*": {
          "&(1,1)": { // row array
            "*": {
              "@": "@(3,header[&1])"
            }
          }
        }
      }
    }
  }
]

where

  • "*s": { stands for rows
  • "&(1,1)": { -> not immediate(zeroth) level but one more level up by using &(1, and grab the value there the first asterisk exists by &(..,1)
  • "@": "@(3,header[&1])" -> 3 levels needed as stated at the right hand side traverse the colon as well in order to reach the level of &(1,1) which is used to represent the "row" array along with &1 representation to go one level up the tree to reach the indexes of the array "row" while matching with the values of "row" through use of @ on the left hand side

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

enter image description here

Upvotes: 1

mattyb
mattyb

Reputation: 12093

Is there a copy/paste error in your input? Judging by your desired output, the second object's header array should be ["header4", "header5", "header6"]. If that's the case, this spec should work:

[
  {
    "operation": "shift",
    "spec": {
      "rows": {
        "*": {
          "header": {
            "*": {
              "*": {
                "@(3,row[#2])": "&"
              }
            }
          }
        }
      }
    }
  }
]

Upvotes: 1

Related Questions