Split JSON into two individual JSON objects using Nifi

5.1k Views Asked by At

I have a JSON like

{
    "campaign_key": 316,
    "client_key": 127,
    "cpn_mid_counter": "24",
    "cpn_name": "Bopal",
    "cpn_status": "Active",
    "clt_name": "Bopal Ventures",
    "clt_status": "Active"
}

Expected output

1st JSON :

{
    "campaign_key": 316,
    "client_key": 127,
    "cpn_mid_counter": "24",
    "cpn_name": "Bopal",
    "cpn_status": "Active"
}

2nd JSON:

{
    "clt_name": "Bopal Ventures",
    "clt_status": "Active"
}

How do I acheive this by using NIFI? Thanks.

2

There are 2 best solutions below

0
On BEST ANSWER

Karthik,

Use EvaluateJsonPath processor to get those all json Values by using its keys.

Example: $.campaign_key for gets compaign key value and $.clt_name for get clt name.

Like above one you can get all jsons.

Then use ReplaceText Processor for convert single json into two jsons.

{"Compaign_Key":${CompaignKey},...etc}
{"Clt_name":${clt_name}}

It will convert single json into two jsons.

Hope this helpful and let me know if you have issues.

5
On

You can do what 'user' had said. The not-so-good thing about that approach is, if you number of fields are increasing, then you are required to add that many JSON Path expression attributes to EvaluateJsonPath and subsequently add that many attributes in ReplaceText.

Instead what I'm proposing is, use QueryRecord with Record Reader set to JsonTreeReader and Record Writer set to JsonRecordSetWriter. And add two dynamic relationship properties as follows:

json1 : SELECT campaign_key, client_key, cpn_mid_counter, cpn_name, cpn_status FROM FLOWFILE

json2 : SELECT clt_name, clt_status FROM FLOWFILE

This approach takes care of reading and writing the output in JSON format. Plus, if you want to add more fields, you just have add the field name in the SQL SELECT statement.

QueryRecord processor lets you execute SQL query against the FlowFile content. More details on this processor can be found here

Attaching screenshots

enter image description here

enter image description here