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
Highlighted

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

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
Highlighted

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

View solution in original post

7 REPLIES 7
Highlighted

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

View solution in original post

Highlighted

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.

Highlighted

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

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

Highlighted

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

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 .

Highlighted

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

Highlighted

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

New Contributor

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.

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

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