I am trying to extract list of string from following Json
"example":{[
{"@name":"name1"},
{"@name":"name2"},
{"@name":"name3"},
{"@name":"name4"}
]}
I want to get the list of @name values. I am using jayway jsonpath to do this.
I tried the following code,
List<String> response = null;
try{
ReadContext ctx = JsonPath.parse(data);
response = ctx.read(xPath);
}catch(Exception e){
AppLogger.EventLogger.error(e.getMessage());
response = null;
}
return response;
but got an exception saying could not find the specified path. I have used the following jsonPath example.@name
Can someone tell me What went wrong in my code? how to extract list @name values.
Expected output :
[name1,name2,name3,name4]
I have made small changes to your json in order to make it valid.
{"example":[
{"@name":"name1"},
{"@name":"name2"},
{"@name":"name3"},
{"@name":"name4"}
]}
and the code which specified works perfectly fine with the following expression: $.example[*].@name
There are a lot of online services which can help validate your json. One of them is jsonlint.com
I would also recommend to look at json.org.