Search code examples
javajsonapache-nifijolt

JOLT Flatten array and nested object


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


Solution

  • 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 http://jolt-demo.appspot.com/ is

    enter image description here

    Edit : If your aim is to assign default values for missing array elements as commented, then replace "reportT*": "&2[&1].&" with "*": "&2[&1].&" considering the case that the array classifications has more elements than the current state.