Home > front end >  Match JSON arrays with JOLT
Match JSON arrays with JOLT

Time:05-20

I have JSON from REST API:

{
  "fields": [
    "advertiser_id",
    "campaign_id",
    "day"
  ],
  "data": [
    [
      "8905",
      "234870",
      "2021-09-28"
    ],
    [
      "5634",
      "88467870",
      "2021-09-28"
    ]
  ]
}

I want to match values inside fields array with values inside data. The have same order. So I expect to get:

[
  {
    "advertiser_id": "8905",
    "campaign_id": "234870",
    "day": "2021-09-28"
  },
  {
    "advertiser_id": "5634",
    "campaign_id": "88467870",
    "day": "2021-09-28"
  }
]

Any ways to do it with JOLT?

CodePudding user response:

You can use a shift transformation spec in which

  • go 4 levels up (traverse once:, and { triple) in order to reach fields array as picking sub-arrays of data array by using [&1]

  • dissipate all returning key-value pairs through use of [&2]. node

such as

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "*": {
          "*": {
            "@": "[&2].@(4,fields[&1])"
          }
        }
      }
    }
  }
]

the demo on the site enter image description here

  • Related