Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find node exists in JSON

Tags:

json

jsonpath

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 ?

like image 301
Malintha Avatar asked Feb 12 '15 13:02

Malintha


2 Answers

    ReadContext ctx = JsonPath.parse("{}", com.jayway.jsonpath.Configuration.defaultConfiguration().addOptions(Option.SUPPRESS_EXCEPTIONS));

    assertThat(ctx.read("$.components"), nullValue());
like image 75
zach ma Avatar answered Oct 16 '22 11:10

zach ma


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
like image 13
Dmytro Pastovenskyi Avatar answered Oct 16 '22 12:10

Dmytro Pastovenskyi