AKHIL KUMAR
AKHIL KUMAR

Reputation: 123

Do json key transformation with apache nifi

I need to do a json transformation in apache nifi. The json keys in the payload would be dynamically generated. For example in the input given below, the 'customer' has attributes 'fname' and 'lname'. I need to change this 'fname' -> 'firstname' and 'lname' -> 'lastname' as provided in the 'mappingvalues'.

Since I am newbie to nifi. I dont know where to start. I have tried some json transformers like jolt. But couldn't achieve the expected result.

The jolt transform that i have used is given below :

[
  {
    "operation": "shift",
    "spec": {
      "customer": {
        "*": {
          "@": "&"
        }
      }
    }
  }
]

which produced an output

{
  "fname" : "akhil",
  "lname" : "kumar"
}

The input and expected output of what I need to achieve is given below :


{
  "customer": {
    "fname": "akhil",
    "lname": "kumar",
    .
    .
    .
  },
  "mappingvalues": {
    "fname": "firstname",
    "lname": "lastname",
    .
    .
    .
  }
}

##OUTPUT
{
  "customer": {
    "firstname": "akhil",
    "lastname": "kumar",
    .
    .
    .
  }
}

*Is there any way to achieve the same in nifi with or without using jolt transform? Is it possible to do the same with groovy script? * Please help me on the same.

Upvotes: 0

Views: 317

Answers (1)

daggett
daggett

Reputation: 28564

the code in groovy with recursive mapping:

import groovy.json.JsonSlurper

def ff = session.get()
if(!ff)return

def json = ff.read().withReader("UTF-8"){r-> new JsonSlurper().parse(r) } 

def mappings = json.remove('mappingvalues')
def mapper(o, mappings){
    if(o instanceof Map){
        //json object. let's iterate it and do mapping
        o = o.collectEntries{k,v-> [ (mappings[k] ?: k), mapper(v,mappings) ] }
    }else if(o instanceof List){
        //map elements in array
        o = o.collect{v-> mapper(v,mappings) }
    }
    return o
}
json = mapper(json,mappings)

ff.write("UTF-8"){w-> new JsonBuilder(json).writeTo(w) }
REL_SUCCESS << ff

Upvotes: 1

Related Questions