Home > Back-end >  JOLT Flatten array and nested object
JOLT Flatten array and nested object

Time:05-31

Given the following JSON

{
  "id": "1234",
  "recordType": "E",
  "receiveDate": "2009-01-01",
  "receiveTime": "09:55:00",
  "releaseDate": "2009-01-02",
  "releaseTime": "08:30:00",
  "classifications": [
    {
      "reportType": 1031435,
      "description": {
        "string": "Progress Report"
      }
    },
    {
      "reportType": 1031435,
      "description": {
        "string": "Net Tangible Asset Backing"
      }
    }
  ],
  "type": "ASX"
}

I need to transform it to

{
  "id": "1234",
  "recordType": "E",
  "classifications": [
    {
      "reportType": 1031435,
      "description": "Progress Report"
    },
    {
      "reportType": 1031435,
      "description": "Net Tangible Asset Backing"
    }
  ],
  "type": "ASX"
}

I'm having great difficulty trying to figure out exactly how to achieve this with Jolt or whether it's even possible at all.

Any help truly appreciated. Ron Milne

CodePudding user response:

You can use a shift transformation such as

[
  {
    "operation": "shift",
    "spec": {
      "id": "&",
      "recordT*": "&",
      "class*": {
        "*": {
          "reportT*": "&2[&1].&",
          "desc*": {
            "*": "&3[&2].&1"
          }
        }
      },
      "type": "&"
    }
  }
]

where * wilcards used to abbreviate the whole literals per each key names such as reportT* represents reportType, and single * wilcard represents the others(else case)

Firstly we reached the innermost attributes( "string":... ) of the whole JSON value through use of "*": { following "classifications" key in order to walk through the indices of it. The "&" wildcard replicates each values for the corresponding attribute, &3 and &2 are used to replicate the array's name(classifications) by going 2 or 3 levels up and grabbing the literal per each, and [&1] or [&2] are used to determine the common factor for constructing the objects of the array.

the demo on the site enter image description here

  • Related