0

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]
2
  • 2
    First of all it doesn't look like valid json... Commented Jul 19, 2017 at 6:57
  • @SouravGanguly, Thanks. but consider it is valid Json :) Commented Jul 19, 2017 at 7:19

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.