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.

Community
  • 1
  • 1
Josh Harrison
  • 434
  • 1
  • 6
  • 18

1 Answers1

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.

James
  • 11,721
  • 2
  • 35
  • 41
  • 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? – Josh Harrison Jan 20 '16 at 20:34
  • 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! – Josh Harrison Jan 20 '16 at 20:53