Search code examples
jsonapache-nifijolt

How to Compare and Extract JSON Array Values to a Specific JSON Structure Output?


I have a JSON object that contains two arrays, idByLine and orderLine. I need to extract values from idByLine, compare them with the values in orderLine, and then generate a JSON output with a specific structure, using Jolt Transform in NiFi. Here's the JSON structure I'm working with:

{
  "orderLine": [
    {
      "ID": "1",
      "Country": "Country1",
      "Email": [
        {
          "email1": "email1"
        },
        {
          "email2": "email2"
        }
      ]
    },
    {
      "ID": "2",
      "Country": "Country2",
      "Email": [
        {
          "email1": "email1"
        },
        {
          "email2": "email2"
        }
      ]
    }
  ],
  "idByLine": [
    {
      "idLine": "1",
      "orderId": "AMD123"
    },
    {
      "idLine": "2",
      "orderId": "AMD555"
    }
  ]
}

I need to make sure that the idLine values of idByLine are present in the output json, with the other fields I leave in the output json example. How can I achieve this using JoltTransfor from nifi apache?

The output JSON structure I need is:

[
 {
    "idLine": "AMD123",
    "ID": "1",
    "Country": "Country1",
    "Email": [
      {
        "email1": "email1"
      },
      {
        "email2": "email2"
      }
    ]
 },
 {
    "idLine": "AMD555",
    "ID": "2",
    "Country": "Country2",
    "Email": [
      {
        "email1": "email1"
      },
      {
        "email2": "email2"
      }
    ]
 }
]

Solution

  • You can use the following transformation:

    [
      {
        "operation": "shift",
        "spec": {
          "idByLine": {
            "*": {
              "orderId": "[&1].idLine"
            }
          },
          "orderLine": {
            "*": {
              "ID": "[&1].ID",
              "Country": "[&1].Country",
              "Email": "[&1].Email"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": ""
        }
      }
    ]
    

    enter image description here