Search code examples
jsonapache-nifijolt

Jolt Json transformation: Set value by reference identifier (not index)


I am trying to create NIFI Jolt transformation where I want to attach some value from another JSON collection by key from original collection (Assign Prefix from EventCodePrefixes by referenced EventCodePrefixId in EventCodes):

Request sample:

{
  "EventCodes": [
    {
      "EventCodePrefixId": 2
    },
    {
      "EventCodePrefixId": 2
    }
  ],
  "EventCodePrefixes": [
    {
      "Id": 1,
      "Prefix": "AA"
    },
    {
      "Id": 2,
      "Prefix": "BB"
    }
  ]
}

Expected result:

{
  "EventCodes": [
    {
      "EventCodePrefixId": 2,
      "EventCodePrefix": "BB"
    },
    {
      "EventCodePrefixId": 2,
      "EventCodePrefix": "BB"
    }
  ],
  "EventCodePrefixes": [
    {
      "Id": 1,
      "Prefix": "AA"
    },
    {
      "Id": 2,
      "Prefix": "BB"
    }
  ]
}

THANK YOU!

Current (Wrong) transformation (How to change it to lookup instead of take index 0 item?):

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "EventCodes": {
        "*": {
          "EventCodePrefix": "@(3,EventCodePrefixes[0].Prefix)"
        }
      }
    }
  }
]

Current (Wrong) result:

{
  "EventCodes": [
    {
      "EventCodePrefixId": 2,
      "EventCodePrefix": "AA"
    },
    {
      "EventCodePrefixId": 2,
      "EventCodePrefix": "AA"
    }
  ],
  "EventCodePrefixes": [
    {
      "Id": 1,
      "Prefix": "AA"
    },
    {
      "Id": 2,
      "Prefix": "BB"
    }
  ]
}

THANK YOU!


Solution

  • The following spec will work for you.

    [
      {
        "operation": "shift",
        "spec": {
          "*": "&",
          "EventCodePrefixes": {
            "@": "&",
            "*": {
              "*": "@1,Id.&"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "EventCodes": {
            "@": "&",
            "*": {
              "EventCodePrefixId": {
                "*": {
                  "@4,&": {
                    "Prefix": "&5[&4].EventCodePrefix"
                  }
                }
              }
            }
          },
          "EventCodePrefixes": "&"
        }
      }
    ]
    

    enter image description here