How to find node exists in JSON

Malintha picture Malintha · Feb 12, 2015 · Viewed 17.6k times · Source

I have following JSON

{"subscription": 
 {
 "callbackReference": "xyz" ,
 "criteria": "Vote",
 "destinationAddress": "3456" ,
  "notificationFormat" : "JSON"
 }
}

I want to check whether "notificationFormat" elements exits there using JSONPath expression. I can get the value of above element using following JSONPath expression.

$.subscription.notificationFormat

Can I use similar kind of expression which returns boolean value checking whether elements exists ?

Answer

Dmytro Pastovenskyi picture Dmytro Pastovenskyi · Feb 12, 2015

If I understood your question correct here is an answer.

This would check if notificationFormat exists in your json.

$.subscription[?(@.notificationFormat)]

That would get all destinationAddress in case if notificationFormat exists

$.subscription[?(@.notificationFormat)].destinationAddress