Search code examples
jsonapache-nifijolt

Dynamic jolt to handle when 2nd element in a array has a nested array


Dynamic Jolt spec to handle when one nested array is present in the 2nd element of the outside array

My jolt spec is working perfectly fine if we don't receive nested array

Some incoming JSONs have an identifiers nested array inside routeSegements array. Some elements does not have

Input :

{
  "shipment": {
    "id": "4f47b29b-0ce1-42cb-8cc9-6c1b87150a74",
    "routeInfo": {
      "routeSegments": [
        {
          "id": "083fe93b-f770-31b3-87e4-caac4a3b01ed",
          "fromStopId": "4652dda5-973a-3bac-a3b1-30d58fe21c6a",
          "toStopId": "c2977ae4-c1f7-3ba4-9bcd-351350fd5c68",
          "transportationMode": "UNKNOWN",
          "routeSegmentSeq": 1
        },
        {
          "id": "5742c90f-34af-3695-8841-7de17f9fbc3c",
          "fromStopId": "c2977ae4-c1f7-3ba4-9bcd-351350fd5c68",
          "toStopId": "21ac1e83-3e9f-3f84-bc8b-4f682ff02dba",
          "transportationMode": "OCEAN",
          "routeSegmentSeq": 2,
          "identifiers": [
            {
              "type": "VOYAGE_NUMBER",
              "value": "KQ321A"
            },
            {
              "type": "VESSEL_NAME",
              "value": "MATSON NIIHAU"
            },
            {
              "type": "VESSEL_IMO",
              "value": "9294159"
            },
            {
              "type": "CARRIER_NAME",
              "value": "DHL"
            }
          ]
        },
        {
          "id": "ef339c18-46e5-3a53-ab53-675f7c2cca17",
          "fromStopId": "21ac1e83-3e9f-3f84-bc8b-4f682ff02dba",
          "toStopId": "9c5a042d-665c-35fa-8ebb-bb98f8f4eeed",
          "transportationMode": "UNKNOWN",
          "routeSegmentSeq": 3
        }
      ]
    }
  }
}

Output Expected :

[
  {
    "shipment_id": "4f47b29b-0ce1-42cb-8cc9-6c1b87150a74",
    "route_segments_id": "083fe93b-f770-31b3-87e4-caac4a3b01ed",
    "route_segments_from_stopId": "4652dda5-973a-3bac-a3b1-30d58fe21c6a",
    "route_segments_to_stopId": "c2977ae4-c1f7-3ba4-9bcd-351350fd5c68",
    "transportation_mode": "UNKNOWN",
    "route_segment_seq_no": 1
  },
  {
    "shipment_id": "4f47b29b-0ce1-42cb-8cc9-6c1b87150a74",
    "route_segments_id": "5742c90f-34af-3695-8841-7de17f9fbc3c",
    "route_segments_from_stopId": "c2977ae4-c1f7-3ba4-9bcd-351350fd5c68",
    "route_segments_to_stopId": "21ac1e83-3e9f-3f84-bc8b-4f682ff02dba",
    "transportation_mode": "OCEAN",
    "route_segments_identifier_type": "VOYAGE_NUMBER",
    "route_segments_identifier_value": "KQ321A",
    "route_segment_seq_no": 2
  },
  {
    "shipment_id": "4f47b29b-0ce1-42cb-8cc9-6c1b87150a74",
    "route_segments_id": "5742c90f-34af-3695-8841-7de17f9fbc3c",
    "route_segments_from_stopId": "c2977ae4-c1f7-3ba4-9bcd-351350fd5c68",
    "route_segments_to_stopId": "21ac1e83-3e9f-3f84-bc8b-4f682ff02dba",
    "transportation_mode": "OCEAN",
    "route_segments_identifier_type": "VESSEL_NAME",
    "route_segments_identifier_value": "MATSON NIIHAU",
    "route_segment_seq_no": 2
  },
  {
    "shipment_id": "4f47b29b-0ce1-42cb-8cc9-6c1b87150a74",
    "route_segments_id": "5742c90f-34af-3695-8841-7de17f9fbc3c",
    "route_segments_from_stopId": "c2977ae4-c1f7-3ba4-9bcd-351350fd5c68",
    "route_segments_to_stopId": "21ac1e83-3e9f-3f84-bc8b-4f682ff02dba",
    "transportation_mode": "OCEAN",
    "route_segments_identifier_type": "VESSEL_IMO",
    "route_segments_identifier_value": "9294159",
    "route_segment_seq_no": 2
  },
  {
    "shipment_id": "4f47b29b-0ce1-42cb-8cc9-6c1b87150a74",
    "route_segments_id": "5742c90f-34af-3695-8841-7de17f9fbc3c",
    "route_segments_from_stopId": "c2977ae4-c1f7-3ba4-9bcd-351350fd5c68",
    "route_segments_to_stopId": "21ac1e83-3e9f-3f84-bc8b-4f682ff02dba",
    "transportation_mode": "OCEAN",
    "route_segments_identifier_type": "CARRIER_NAME",
    "route_segments_identifier_value": "DHL",
    "route_segment_seq_no": 2
  },
  {
    "shipment_id": "4f47b29b-0ce1-42cb-8cc9-6c1b87150a74",
    "route_segments_id": "ef339c18-46e5-3a53-ab53-675f7c2cca17",
    "route_segments_from_stopId": "21ac1e83-3e9f-3f84-bc8b-4f682ff02dba",
    "route_segments_to_stopId": "9c5a042d-665c-35fa-8ebb-bb98f8f4eeed",
    "transportation_mode": "UNKNOWN",
    "route_segment_seq_no": 3
  }
]

Jolt Spec i tried :

[
  {
    "operation": "shift",
    "spec": {
      "shipment": {
        "id": "[&1].shipment_id",
        "routeInfo": {
          "routeSegments": {
            "*": {
              "id": "[&1].route_segments_id",
              "fromStopId": "[&1].route_segments_from_stopId",
              "toStopId": "[&1].route_segments_to_stopId",
              "transportationMode": "[&1].transportation_mode",
              "identifiers": {
                "*": {
                  "type": "[&1].route_segments_identifier_type",
                  "value": "[&1].route_segments_identifier_value"
                }
              },
              "routeSegmentSeq": "[&1].route_segment_seq_no"
            }
          }
        }
      }
    }
  }
]

Right now its not coming properly.

Can anyone who is a jolt expert, help me get the desired output. I think i m stuck in the last step


Solution

  • You can add one more grouping level which is the value of routeSegmentSeq ( @1,routeSegmentSeq. or @3,routeSegmentSeq. ) that prefixes the identiers on the right hand side such as

    [
      {
        "operation": "shift",
        "spec": {
          "@shipment.routeInfo.routeSegments": {
            "*": {
              "@2,shipment.id": "@1,routeSegmentSeq.&1.shipment_id",
              "id": "@1,routeSegmentSeq.&1.route_segments_id",
              "fromStopId": "@1,routeSegmentSeq.&1.route_segments_from_stopId",
              "toStopId": "@1,routeSegmentSeq.&1.route_segments_to_stopId",
              "routeSegmentSeq": "@1,routeSegmentSeq.&1.route_segment_seq_no",
              "transportationMode": "@1,routeSegmentSeq.&1.transportationMode",
              "identifiers": {
                "*": {
                  "@4,shipment.id": "@3,routeSegmentSeq.&1.shipment_id",
                  "@2,id": "@3,routeSegmentSeq.&1.route_segments_id",
                  "@2,fromStopId": "@3,routeSegmentSeq.&1.route_segments_from_stopId",
                  "@2,toStopId": "@3,routeSegmentSeq.&1.route_segments_to_stopId",
                  "*": "@3,routeSegmentSeq.&1.route_segments_identifier_&",
                  "@2,routeSegmentSeq": "@3,routeSegmentSeq.&1.route_segment_seq_no",
                  "@2,transportationMode": "@3,routeSegmentSeq.&1.transportationMode"
                }
              }
            }
          }
        }
      },
      { // get rid of object keys
        "operation": "shift",
        "spec": {
          "*": {
            "*": ""
          }
        }
      },
      { // pick only single one from repeating identical values
        "operation": "cardinality",
        "spec": {
          "*": {
            "*": "ONE"
          }
        }
      }
    ]
    

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

    enter image description here