Skip to main content
Olivia_4692
Gold ⭐️
March 6, 2022
Solved

How to compare few attributes in a json file and then route if matched/unmatched using NiFi?

I have a nested json with few first level keys. I want to compare two keys before and after in that json to see if the states are matching or not. If the states match then route to update api else insert api.

What I have tried so far : getfile-->evaluate json path(to get the two keys before and after)-->route on attributes

Now my problem is that route on attributes always evaluates to unmatched even if the values in eval json path for before and after are the same.

Expression : Eval json path 1 : a_before = $.['a']['before'][*]['ip']

Eval json path 2 : a_after = $.['a']['after'][*]['ip']

Above results in the same ip - ["1.0.0.0"]

RouteOnAttribute : ${a_after:equals(${a_before})}

This always results in an unmatched even if the records match.

Any help will be appreciated.

araujo
araujoAnswer
Platinum ⭐️⭐️⭐️
March 7, 2022

Hi @Olivia_4692 ,

 

The only issue I can see here is that in your sample data "Before" has a capital B, while you your EvaluateJSONPath configuration it's all in lowercase. This will certainly create a problem for your logic.

 

I tried your data (converting "Before" to "before") and it worked correctly in my test.

 

Please my test flow attached.

 

Cheers,

André

 

--

Was your question answered? Make sure to mark the answer as the accepted solution.
If you find a reply useful, say thanks by clicking on the thumbs up button.

-- Was your question answered? Please take some time to click on "Accept as Solution" below this post. If you find a reply useful, say thanks by clicking on the thumbs up button.

7 replies

araujo
Platinum ⭐️⭐️⭐️
Platinum ⭐️⭐️⭐️
March 6, 2022

@Olivia_4692 ,

 

Your approach is correct and it should work as you expect.

There are a few things that I'd like to check, though:

 

1. In the JSON Path expression "a_before = $.['a']['before'][]['ip']", what's the reason for the empty "[]" just before "['ip']"? Could you please share a sample of your data?

 

2. Have you checked for leading or trailing spaces in either one of the attributes?

 

3. This is probably a copy&paste mistake, but I noticed that your RouteOnAttribute expression is missing a "}" at the very end.

 

Cheers,

Andre

 

--

Was your question answered? Make sure to mark the answer as the accepted solution.
If you find a reply useful, say thanks by clicking on the thumbs up button.

Translate
-- Was your question answered? Please take some time to click on "Accept as Solution" below this post. If you find a reply useful, say thanks by clicking on the thumbs up button.
Olivia_4692
Gold ⭐️
March 7, 2022

Hi Andre,

thank you for your answer. 

1. The empty [] is actually [*] because that value keeps changing. It think that did not get copied. 
2.I’ve checked for space and there are none

3. Yes thank you for pointing that out. And sorry about that. I was a copy paste mistake.

 

I've just edited my OP.

Translate
araujo
Platinum ⭐️⭐️⭐️
araujoAnswer
Platinum ⭐️⭐️⭐️
March 7, 2022

Hi @Olivia_4692 ,

 

The only issue I can see here is that in your sample data "Before" has a capital B, while you your EvaluateJSONPath configuration it's all in lowercase. This will certainly create a problem for your logic.

 

I tried your data (converting "Before" to "before") and it worked correctly in my test.

 

Please my test flow attached.

 

Cheers,

André

 

--

Was your question answered? Make sure to mark the answer as the accepted solution.
If you find a reply useful, say thanks by clicking on the thumbs up button.

Translate
-- Was your question answered? Please take some time to click on "Accept as Solution" below this post. If you find a reply useful, say thanks by clicking on the thumbs up button.