Support Questions

Find answers, ask questions, and share your expertise

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

avatar
Rising Star

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

avatar

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

View solution in original post

7 REPLIES 7

avatar

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

avatar

Another solution would be:

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

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

avatar
Rising Star

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

avatar
Rising Star

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 .

avatar

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

avatar
Explorer

I have some sort of the same scenario. But I have to route out on department value, like department="IT" route to some other flow and for department="DB" route to another flow.

 

For that, I have written out expression as  $.[?(@.department=="DB")]. But this doesn't work. Please suggest for the above scenario.

 

And also if we have to apply some conditions like route data for a>10 like that.

avatar
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