Support Questions

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

Check Array or Object in JSON

avatar
Explorer

Hello everybody. In order to use a JOLT specification, I need to check ,inside my JSON, if a specific key is an Array or an Object and then route on an attribute, to be set after the validation. I tried different ways, through json path evaluators, attributes and so on and came to the conclusion that the only right way to do it is by executing a script, right? Has someone ever come across the same problem? If yes, could you provide some examples? Thanks!

5 REPLIES 5

avatar
Rising Star

I assume this is being done in Nifi? (I would add the Apache-Nifi tag if so)

If you know the structure of the JSON object that you will be receiving, you can use an evaluateJSONPath processor to pull the JSON key's value into a flowfile attribute. Then an updateAttribute processor where you use Nifi expression language's 'equals' to determine if the value is as you expected.

https://nifi.apache.org/docs/nifi-docs/html/expression-language-guide.html#equals

The output of updateAttribute will be true or false and you can pass that to your routeOnAttribute processor.

example.jpg

I hope this helps or gives you some ideas!

avatar
Explorer
@Nick Lewis

thanks for your answer but I don't think it would resolve my problem. I know the JSON structure except for one of the objects.

I'm facing 2 different situations, I'll make an example in pseudo-language:

1) "key":[{obj},{obj}...{obj}]

2) "key":{obj}

I wasn't able to produce a proper JOLT spec, cause I need to loop inside the array in case 1).
I was able to find a different solution though.

Using an ExecuteScript processor and an ECMAScript (using Array.isArray()) then setting a proper attribute in the Flowfile, then routing to different JOLT spec, I'm able to handle case 1) an 2) separately.

This is actually working pretty well but I'm still wondering if I could handle the 2 cases inside one JOLT spec.

(I edited the tags, sorry :))

avatar
Rising Star

No problem on the tags, I just wanted to ensure you got some traffic on this!

I had never looked into JOLT before now, this is some pretty cool stuff! I'll look into it and maybe I'll be able to come up with something for you. Sounds like you have got your minimum viable product for now though:P

avatar
New Contributor

I have used "RouteOnContent" and it works fine:

94429-routeoncontent.png

avatar
New Contributor

This is a great solution! Makes it very easy for others to follow exactly what is happening!