Support Questions

Find answers, ask questions, and share your expertise

Need to display each element of array in a separate row using nifi processor

avatar
Explorer

Hi Team ,

My flow file content like

[{"Id":"43","name":"ABC"},{"Id":"44","name":"Xyz"}]

I need data as below .Please suggest which processor helps us ..

{"Id":"43","name":"ABC"}{"Id":"44","name":"Xyz"}

Thanks,

Siva v

3 REPLIES 3

avatar
Master Guru

@siva vulli

You can use split json processor, this processor will split json array of messages into individual messages as content of each flowfile i.e if your json array having 100 messages in it then split json processor splits relation will output 100 flowfiles having each message in it.

64955-splitjson.png

JsonPath Expression

$.*

with this configuration we are splitting json array into individual messages.

Input to splitjson processor:-

[{"Id":"43","name":"ABC"},{"Id":"44","name":"Xyz"}]

Output from splitjson processor:-

We are going to have 2 flowfiles(as we are having 2 json messages in array)

flowfile1:-

{"Id":"43","name":"ABC"}

flowfile2:-

{"Id":"44","name":"Xyz"}

As you mentioned in the question your expected output would be {"Id":"43","name":"ABC"}{"Id":"44","name":"Xyz"}

for this case after splitjson processor use merge content processor

64956-mergecontent.png

now in this merge content processor we are merging all the splits into one flowfile by using merge strategy as Defragment

Use Merged relation from merge content processor which will give your desired results.

Output from Merge Content processor:-

{"Id":"43","name":"ABC"}{"Id":"44","name":"Xyz"}

Flow:-

64958-flow-mergecontent.png

1.SplitJson processor //for splitting arrays into individual messages
2.Merge content //to merge the splitted messages into one flowfile content using defragment strategy

(or)

Method2:-

By using Replace text processor:-

{"Id":"43","name":"ABC"}{"Id":"44","name":"Xyz"}

Search Value

\[(.*?)\]

Replacement Value

$1

Maximum Buffer Size

1 MB //needs to change this value if your json message size is more than 1 MB

Replacement Strategy

Regex Replace

Evaluation Mode

Entire text

In this processor we are capturing all the json message without [](squarebrackets) and replacing that as flowfile content

Input:-

[{"Id":"43","name":"ABC"},{"Id":"44","name":"Xyz"}]

Output:-

Now in output flowfile content we are not having square brackets.

{"Id":"43","name":"ABC"},{"Id":"44","name":"Xyz"}

Use another Replace text processor to replace "}, with "}

Now we are literally checking for the "}, in content and replacing with "}

Configs:-

Search Value

"},

Replacement Value

"}

Maximum Buffer Size

1 MB //needs to change this value if your json message size is more than 1 MB

Replacement Strategy

Literal Replace

Evaluation Mode

Entire text

Input flowfile:-

{"Id":"43","name":"ABC"},{"Id":"44","name":"Xyz"}

Output Flowfile:-

{"Id":"43","name":"ABC"}{"Id":"44","name":"Xyz"}

Flow:-

64957-flow-replacetext.png

1.Replacetext processor //to capture all the content of flowfile without square brackets([])
2.Replacetext processor //replace "}, with "}

In both methods we are going to have same output as you can decide which method best fits for your case.

Let us know if you are having any issues ..!!

avatar
Master Guru
@siva vulli

Does the answer helped to resolve your issue, Click on Accept button below to accept the answer,to close the thread and it would be great help to Community users to find solution quickly for these kind of issues.

avatar

@Shu This solution doesn't work if I have multiple arrays in one json message. Example - {"TxnMessage": {"HeaderData": {"EventCatg": "F"},"PostInrlTxn": {"Key": {"Acnt": "1234567890","Date": "20181018"},"Id": "3456","AdDa": {"Area": [{"HgmId": "","HntAm": 0},{"HgmId": "","HntAm": 0}]},"escTx": "Reload","seTb": {"seEnt": [{"seId": "CAKE","rCd": 678},{"seId": "","rCd": 0}]},"Bal": 6766}}}

If you could help with analysis and/or pointers on how each element of array in such input can be split into a separate rows?