3

I am attempting to use the EvaluateJsonPath processor in Nifi, and am having trouble with the jayway jsonpath syntax. My object looks like the following:

{"text":"my stuff", "tags":["abc", "xyz", "beq"]}

I want to route messages based on the tags - I want everything containing "xyz" to be routed one way, and everything not containing it to be routed another way. Using http://jsonpath.herokuapp.com/ I've been testing and trying to figure out the syntax to filter based on a json object containing an array of strings matching. I can match based on overt index (so $.[?(@.tags[1] =~ /xyz/i)] works just fine), but I can't guarantee the order or number of objects in the tags field.

Is there a way to do this in the jayway json module? I saw filter the Json according to string in an array in JSONPATH which I've tried, but it doesn't appear to work in the simulator above.

1 Answer 1

3

I do not know how to do this in one EvaluateJsonPath processor step. But it can certainly be done in a two-step process:

  1. Use EvaluateJsonPath to filter "xyz" tags out of the tags array, using a JsonPath expression like $.tags[?(@ =~ /xyz/i)] and setting the processors return-type to json so an array may be returned. This will result in ["xyz"] for a match and [] for non-matching files
  2. Use RouteOnAttribute to route based on the resulting array, with an expression like ${matchingTags:toLower():contains('xyz')}.

It might also be worth considering evaluating the JSON as text against a regular expression to match the tag.

Sign up to request clarification or add additional context in comments.

2 Comments

Definitely getting closer - I'm a nifi newbie so I'm having a little trouble wrapping my head around the full flow implications. When I set up EvaluateJsonPath to filter tags, I can verify that I'm getting an array that is either empty or contains the tag I'm looking for. I then flow "matched" into RouteOnAttribute, but it's just the single array that is empty or contains my tag - I'm hoping to route the whole flow file (the entire JSON object) to the appropriate processors after determining the tag exists?
Playing around a little more I realized that I had "destination" set to "flowfile-content" instead of "flowfile-attribute". Looks like we're routing properly now! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.