Search code examples
jsonapache-nifijolt

Apache Nifi - JOLT specification for array of json


I have a JSON flowfile which contains

[
  {
    "Ids": 9,
    "totalOut": 2,
    "outboundOut": 3,
    "timestamp": 98765
  },
  {
    "Ids": 34,
    "totalOut": 7,
    "outboundOut": 9,
    "timestamp": 98760
  },
  {
    "Ids": 54,
    "totalOut": 1,
    "outboundOut": 3,
    "timestamp": 98763
  }
]

I need to sum the totalOut's value, outboundOut's value seperately and i need to sum totalOut's value, outboundOut's value and enrich it as

{
  "totalout_sum": 10,
  "outbound_sum": 15,
  "total_sum": 25
}

Help me to resolve this issue.


Solution

  • You can use the following transformation spec

    [
      { // convert attributes suffixed with "Out" to components of
        // the new independent arrays
        "operation": "shift",
        "spec": {
          "*": {
            "total*": "totalout_sum",
            "outbound*": "outbound_sum"
          }
        }
      },
      { // sum of the components of the array to generate desired attributes
        // namely "totalout_sum" and "outbound_sum"
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": "=intSum(@(1,&))"
        }
      },
      { // generate total sum of the values of the newly generated attributes
        "operation": "modify-overwrite-beta",
        "spec": {
          "total_sum": "=intSum(@(1,totalout_sum),@(1,outbound_sum))"
        }
      }
    ]
    

    the demo on the site is

    enter image description here