Search code examples
arraysjsonapache-nifijolt

How to compare two lists having json in a single large list and map it using common column key in NiFi using jolt transform


I have a list of json object having two arrays after merge content. Both the arrays are having one common column ID.I want to create a nested array based on that id column.

JSON Array input:

[
  [
    {
      "ID": "1",
      "A1": "test",
      "B1": "test1"
    }
  ],
  [
    {
      "SubID": "1",
      "ID": "1",
      "C1": "test2",
      "D1": "test3"
    },
    {
      "SubID": "2",
      "ID": "1",
      "C1": "test4",
      "D1": "test5"
    }
  ]
]

Expected Output:

{
  "Header": {
    "ID": "1",
    "A1": "test",
    "B1": "test1",
    "Details": [
      {
        "SubID": "1",
        "C1": "test2",
        "D1": "test3"
      },
      {
        "SubID": "2",
        "C1": "test4",
        "D1": "test5"
      }
    ]
  }
}

Solution

  • Considering you principally want to group the objects by the existence or non-existence of SubID attribute in it you can use the following spec

    [
      {  
        "operation": "modify-default-beta",
        "spec": {
          "*": {
            "*": {
              "~SubID": "NoSubID" // if SubID doesn't exist, then assign a default value to it 
            }
          }
        }
      },
      { // group all objects by ID while conditionally separating the values of SubID whether equals NoSubID or NOT
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "SubID": {
                "NoSubID": {
                  "@2": "@3,ID.Header"
                },
                "*": {
                  "@2": "@3,ID.Header.Details[]"
                }
              }
            }
          }
        }
      },
      { // get rid of objects keys
        "operation": "shift",
        "spec": {
          "*": ""
        }
      },
      { // get rid of extra attributes
        "operation": "remove",
        "spec": {
          "*": {
            "SubID": "",
            "Details": {
              "*": {
                "ID": ""
              }
            }
          }
        }
      }
    ]