3

I have a JSON response which has root as an array of 1 or more objects. I want to extract the value of one of the elements within each object.

Here is the JSON sample:

[  
   {  
      "od_pair":"7015400:8727100",
      "buckets":[  
         {  
            "bucket":"C00",
            "original":2,
            "available":2
         },
         {  
            "bucket":"A01",
            "original":76,
            "available":0
         },
         {  
            "bucket":"B01",
            "original":672,
            "available":480
         }
      ]
   },
   {  
      "od_pair":"7015400:8814001",
      "buckets":[  
         {  
            "bucket":"C00",
            "original":2,
            "available":2
         },
         {  
            "bucket":"A01",
            "original":40,
            "available":40
         },
         {  
            "bucket":"B01",
            "original":672,
            "available":672
         },
         {  
            "bucket":"B03",
            "original":632,
            "available":632
         },
         {  
            "bucket":"B05",
            "original":558,
            "available":558
         }
      ]
   }
]

I want to access the values of od_pair within each object.

I tried referring to the root array as $ but that did not help.

This is the code snippet I have written:

        List<Object> LegList = jsonPath.getList("$");
        int NoofLegs = LegList.size();
        System.out.println("No of legs :" +NoofLegs);
        for (int j=0; j<=NoofLegs;j++) {
            String OD_Pair = jsonPath.param("j", j).getString("[j].od_pair");
            System.out.println("OD Pair: " + OD_Pair);
            List<Object> BucketsList = jsonPath.param("j", j).getList("[j].buckets");

            int NoOfBuckets = BucketsList.size();
            System.out.println("no of Buckets: " + NoOfBuckets);
        }

This is the error that I see:

Caused by: 
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup 
failed:
Script1.groovy: 1: unexpected token: [ @ line 1, column 27.
restAssuredJsonRootObject.[j].od_pair

Can someone kindly help me here please?

1 Answer 1

1

You were right to start with the $. However, What you get with your particular JSON is List of HashMap<String, Object> where each JSON Object is represented as a single HashMap. Knowing that you can obtain the list of HashMaps like this:

List<HashMap<String, Object>> jsonObjectsInArray = path.getList("$");

The String will be the name of the attribute. The Object will be either String, Integer, JSONObject or JSONArray. The latter isn't exact class names but it's not relevant to you to achieve desired results.

Now, all we have to do is iterate over the HashMap and extract values of od_pair like this:

for (HashMap<String, Object> jsonObject : jsonObjectsInArray) {
    System.out.println(jsonObject.get("od_pair"));
}

The output is:

7015400:8727100
7015400:8814001

Hope it helps!

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

6 Comments

Thanks for this. However, I have a nested array of buckets presents as well and what would I need to do to access the bucket elements? Let me paste the other code bits too so its more clearer. My requirement is for each od_pairs, I need to capture every bucket codes and their available number:
@Mihir Can you create another question with the given requirement? That would be much clearer for future readers
Thanks for this. However, I have a nested array of buckets presents as well and what would I need to do to access the bucket elements? Let me paste the other code bits too so its more clearer. My requirement is for each od_pairs, I need to capture every bucket codes and their available number. I shall paste the entire code here in a new comment for your reference. If you could guide me on that please?
@Mihir I can but I would suggest asking a new question.
stackoverflow.com/questions/57272470/… @Fenio: Created a new question.
|

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.