Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

Nifi processor - route json array into multiple json arrays based on condition on json filed/attribute

Solved Go to solution

Nifi processor - route json array into multiple json arrays based on condition on json filed/attribute

New Contributor

Is there any Nifi processor to route mulitple json arrays(by splitting json array based on field/attribute condition) to multiple processor based on json field condition .

ex : assume [{a:1},{a:1},{a:2},{a:2}] is jsonarray , there is a requiremnt to route a=1 contained json files are send to another processor as array . and a=2 json array to another processor

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Nifi processor - route json array into multiple json arrays based on condition on json filed/attribute

Hello,

I hope I understand your question properly.

If you want to split the example JSON into two flow-files, each contains an array of elements with a=1, or a=2, then following flow can do the job. The point is passing the same JSON flow-file into two EvaluateJsonPath processors, then each EvaluateJsonPath extract array based on its own interest, using JSON path like '$.[?(@.a=='2')]'.

I've tested with an input JSON:

[{"a":1, "i": 1},{"a":1, "i": 2},{"a":2, "i": 3},{"a":2, "i": 4}]

Then, I got on left side:

[{"a":1,"i":1},{"a":1,"i":2}]

on right side:

[{"a":2,"i":3},{"a":2,"i":4}]

Hope this helps.

8506-nifi-evaluate-json-flow.png

8507-nifi-evaluate-json-config.png

6 REPLIES 6

Re: Nifi processor - route json array into multiple json arrays based on condition on json filed/attribute

Hello,

I hope I understand your question properly.

If you want to split the example JSON into two flow-files, each contains an array of elements with a=1, or a=2, then following flow can do the job. The point is passing the same JSON flow-file into two EvaluateJsonPath processors, then each EvaluateJsonPath extract array based on its own interest, using JSON path like '$.[?(@.a=='2')]'.

I've tested with an input JSON:

[{"a":1, "i": 1},{"a":1, "i": 2},{"a":2, "i": 3},{"a":2, "i": 4}]

Then, I got on left side:

[{"a":1,"i":1},{"a":1,"i":2}]

on right side:

[{"a":2,"i":3},{"a":2,"i":4}]

Hope this helps.

8506-nifi-evaluate-json-flow.png

8507-nifi-evaluate-json-config.png

Re: Nifi processor - route json array into multiple json arrays based on condition on json filed/attribute

Another solution would be:

SplitJson -> EvaluateJsonPath (store a on attribute) -> RouteOnAttribute -> MergeContent

But if the desired output is JSON array, using EvaluateJsonPath is simpler.

Re: Nifi processor - route json array into multiple json arrays based on condition on json filed/attribute

New Contributor

thank you . is it possible add 2 condition for content like a=='1 ' or a=='2' (or) a=='1' and i=='ss' . Thanks in advance

Re: Nifi processor - route json array into multiple json arrays based on condition on json filed/attribute

New Contributor

what is this expression languauge : $.[?(@.a...] , is it jave regex ? how can we use 2 values filter OR or AND for content filed ?? like a==1 or a==2 .

Re: Nifi processor - route json array into multiple json arrays based on condition on json filed/attribute

Hello, sorry for the delay. The language is JSONPath. JSONPath itself has filtering capabilities. Many examples can be found on this page. https://github.com/jayway/JsonPath

Re: Nifi processor - route json array into multiple json arrays based on condition on json filed/attribute

New Contributor

how to append values for empty flowfile attributes for JSON format data. i.e suppose i have 40 columns with JSON data consuming from kafka queue and i have values for 25 fields only and remaining 15 fields, i don't have values .So i put the json into EvaluateJsonPath processor and converted into flowfile attributes ..From some other processor i am getting values for those empty attributes so i need to append/fill the empty attributes with the values with orginal json file.

Can you please suggest how to use it?

thanks

Don't have an account?
Coming from Hortonworks? Activate your account here