Search code examples
jsonapache-nifijolt

Flattening a nested JSON using jolt transform


I am facing a problem, transforming a very complex nested JSON using jolt transformation. Input and output detail is given below.

Input

[
  {
    "metadata": {
      "event_id": "0635FCAD8AEA1EEDB6868EEFA4D25D34",
      "event_key": "OutboundScheduling_0DE1_040D",
      "event_name": "OutboundScheduling_Update",
      "event_source": "RS4_003",
      "event_time": "2023-04-11T06:45:37.4124Z",
      "event_version": "1.0"
    },
    "payload": {
      "departuresite": "0DE1",
      "shippingpoint": "040D",
      "leadtimes": {
        "transportationlt": [
          {
            "destinationsite": "0DE2",
            "destinationlt": [
              {
                "shippingcondition": "01",
                "destinationsitelt": 2
              },
              {
                "shippingcondition": "03",
                "destinationsitelt": 2
              },
              {
                "shippingcondition": "GR",
                "destinationsitelt": 2
              },
              {
                "shippingcondition": "OP",
                "destinationsitelt": 2
              },
              {
                "shippingcondition": "OS",
                "destinationsitelt": 2
              },
              {
                "shippingcondition": "OX",
                "destinationsitelt": 2
              },
              {
                "shippingcondition": "OY",
                "destinationsitelt": 2
              }
            ]
          }
        ],
        "dncreationleadtimes": [
          {
            "distributionchannel": "70",
            "ordertype": "ZU25",
            "orderreason": "DRP",
            "dncreationlt": 1
          }
        ],
        "dnprocessingleadtimes": [
          {
            "distributionchannel": "70",
            "ordertype": "ZU25",
            "orderreason": "DRP",
            "dnprocessinglt": 1
          }
        ],
        "dnloadingleadtimes": [
          {
            "distributionchannel": "70",
            "shippingcondition": "OU",
            "ordertype": "ZU25",
            "orderreason": "DRP",
            "dnloadinglt": 1
          }
        ],
        "vasleadtimes": []
      }
    }
  }
]

Output Expected

[
  {
    "departuresite": "0DE1",
    "shippingpoint": "040D",
    "destinationsite": "0DE2",
    "distributionchannel": "70",
    "ordertype": "ZU25",
    "orderreason": "DRP",
    "dncreationlt": 1,
    "dnloadinglt": 1
  }
]

Jolt spec i m using

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "payload": {
          "leadtimes": {
            "transportationlt": {
              "*": {
                "destinationlt": {
                  "*": {
                    "@5,departuresite": "&3[#2].departuresite",
                    "@5,shippingpoint": "&3[#2].shippingpoint",
                    "@2,destinationsite": "&3[#2].destinationsite",
                    "@4,dncreationleadtimes[&].distributionchannel": "&3[#2].distributionchannel",
                    "@4,dncreationleadtimes[&].ordertype": "&3[#2].ordertype",
                    "@4,dncreationleadtimes[&].orderreason": "&3[#2].orderreason",
                    "@4,dncreationleadtimes[&].dncreationlt": "&3[#2].dncreationlt",
                    "@4,dnloadingleadtimes[&].dnloadinglt": "&3[#2].dnloadinglt"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  { // get rid of the object keys generated within the previous spec
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]

But its not coming as expected.

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


Solution

  • You just can pick the first object from the array by adding a shift transformation as below

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "payload": {
              "leadtimes": {
                "transportationlt": {
                  "*": {
                    "destinationlt": {
                      "*": {
                        "@5,departuresite": "[#2].departuresite",
                        "@5,shippingpoint": "[#2].shippingpoint",
                        "@2,destinationsite": "[#2].destinationsite",
                        "@4,dncreationleadtimes[&].distributionchannel": "[#2].distributionchannel",
                        "@4,dncreationleadtimes[&].ordertype": "[#2].ordertype",
                        "@4,dncreationleadtimes[&].orderreason": "[#2].orderreason",
                        "@4,dncreationleadtimes[&].dncreationlt": "[#2].dncreationlt",
                        "@4,dnloadingleadtimes[&].dnloadinglt": "[#2].dnloadinglt"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "0": { // stands for the first index
            "@": "[]" // square-bracketed nest the first object of the array
          }
        }
      }
    ]
    

    where [#2] not need to be qualified with anything(eg. &3 is not needed)

    or directly apply zeroth index such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "payload": {
              "leadtimes": {
                "transportationlt": {
                  "*": {
                    "destinationlt": {
                      "0": { // apply change here only
                        "@5,departuresite": "[#2].departuresite",
                        "@5,shippingpoint": "[#2].shippingpoint",
                        "@2,destinationsite": "[#2].destinationsite",
                        "@4,dncreationleadtimes[&].distributionchannel": "[#2].distributionchannel",
                        "@4,dncreationleadtimes[&].ordertype": "[#2].ordertype",
                        "@4,dncreationleadtimes[&].orderreason": "[#2].orderreason",
                        "@4,dncreationleadtimes[&].dncreationlt": "[#2].dncreationlt",
                        "@4,dnloadingleadtimes[&].dnloadinglt": "[#2].dnloadinglt"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    ]