2

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?

Fenio
  • 3,528
  • 1
  • 13
  • 27
Mihir
  • 491
  • 5
  • 21

1 Answers1

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!

Fenio
  • 3,528
  • 1
  • 13
  • 27
  • 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 Jul 30 '19 at 12:50
  • @Mihir Can you create another question with the given requirement? That would be much clearer for future readers – Fenio Jul 30 '19 at 12:54
  • 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 Jul 30 '19 at 12:56
  • @Mihir I can but I would suggest asking a new question. – Fenio Jul 30 '19 at 12:57
  • https://stackoverflow.com/questions/57272470/accessing-jsonpath-elements-with-nested-objects @Fenio: Created a new question. – Mihir Jul 30 '19 at 13:07
  • @Mihir Thank you. I'll see if I can help you – Fenio Jul 30 '19 at 13:07