Support Questions

Find answers, ask questions, and share your expertise

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

avatar
Contributor

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.

1 ACCEPTED SOLUTION

avatar
Super Guru

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.

View solution in original post

7 REPLIES 7

avatar
Super Guru

@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.

--
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.

avatar
Contributor

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.

avatar
Super Guru

@Olivia_4692 ,

 

Would you have a sample of the input data?

 

André

 

--
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.

avatar
Contributor

@araujo 

Sample input data :

“a”:{

    “after”:{

        “a123”:{

                 “ip”:”1.0.0.0”,

                 “series”:”4500”

                   }

           },

“Before”:{

        “a123”:{

            “ip”:”1.0.0.0”,

            “series”:”4500

                  }

            }

}

avatar
Super Guru

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.

avatar
Contributor

Hi @araujo - you've attached a text file. How do I import that as a template? If you could attach your flow as an xml file that would be helpful.

avatar
Contributor

@araujo - thank you so much. I referred to the same flow file you have attached. It works perfectly. The problem with mine was I was using two eval json processors and route on attributes wasn't able to compare from two different flowfiles.