Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

how to use evaluate JSON path in nifi

avatar
Expert Contributor

I have streaming JSON data and based on value of a key in JSOn , I need to decide what location I put that JSON content in.

Could I get some directions for this please. I have 4 different values for a key and so the JSON needs to be placed in 4 diff. locations

1 ACCEPTED SOLUTION

avatar
Rising Star

You would create a dynamic attribute to select the key of interest. Consider the following sample doc:

{
 "key-1": "value 1",
 "key-2": "value 2",
 "key-3": "value 3"
 }

In this case, if we needed to route based on the value of key-2, we could create a property in the processor that is

$.key-2

and assign this to some name, such as "routing.value".

With that bit of information extracted as an attribute, we can feed the flowfiles from the EvaluateJsonPath processor. If the locations you mention are file locations, we could potentially simply use a PutFile with expression language to specify a path making use of the routing.value by defining Directory to use the attribute with something like "/path/to/my/data/${routing.value}"

A more powerful and flexible case would be where each flowfile gets sent to a RouteOnAttribute processor after EvaluateJsonPath. In this case, we could define routes for each of the cases and allow them to be sent on to other NiFi components. For instance, maybe some things go to disk and others go to JMS. We can create relationships for RouteOnAttribute that will then allow us to connect each type to its respective processing path.

View solution in original post

2 REPLIES 2

avatar
Rising Star

You would create a dynamic attribute to select the key of interest. Consider the following sample doc:

{
 "key-1": "value 1",
 "key-2": "value 2",
 "key-3": "value 3"
 }

In this case, if we needed to route based on the value of key-2, we could create a property in the processor that is

$.key-2

and assign this to some name, such as "routing.value".

With that bit of information extracted as an attribute, we can feed the flowfiles from the EvaluateJsonPath processor. If the locations you mention are file locations, we could potentially simply use a PutFile with expression language to specify a path making use of the routing.value by defining Directory to use the attribute with something like "/path/to/my/data/${routing.value}"

A more powerful and flexible case would be where each flowfile gets sent to a RouteOnAttribute processor after EvaluateJsonPath. In this case, we could define routes for each of the cases and allow them to be sent on to other NiFi components. For instance, maybe some things go to disk and others go to JMS. We can create relationships for RouteOnAttribute that will then allow us to connect each type to its respective processing path.

avatar
Expert Contributor

Awesome! Thank you.