1

I have a JSON response which I want to parse and extract the data from. Here is the JSON response

[  
 {  
  "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 extract each od_pair and the values of of bucket and available within them.

@Fenio's solution in Accessing jsonpath elements with nested objects has the best approaches. The code snippet that I have refactored looks like this:

List<HashMap<String, Object>> LegList = jsonPath.getList("$");

     for (HashMap<String, Object> singleLeg : LegList) {
        String OD_pair = (String) singleLeg.get("od_pair");

    //List<HashMap<String, Object>> bucketsList = jsonPath.param("j", j).getList("[j].buckets");
        List<HashMap<String, Object>> bucketsList = jsonPath.getList("singleLeg.buckets");

        for (HashMap<String, Object> singleBucket : bucketsList) {
            String BucketCode = (String) singleBucket.get("bucket");
            String Available = (String) 
singleBucket.get("available");

I want to verify if the bucketsList that I am extracting is correct. Earlier I used a for loop with the parameter j. But with this approach which is lot more cleaner and nicer, I wish to understand if I am right in the way am extracting the bucketsList

Mihir
  • 491
  • 5
  • 21

1 Answers1

1

I managed to resolve this. I understood where I was going wrong. Replacing

 List<HashMap<String, Object>> bucketsList = jsonPath.getList("singleLeg.buckets");

with this

List<HashMap<String, Object>> bucketsList = (List<HashMap<String, Object>>) singleLeg.get("buckets");

Has resolved my issue and now things work as expected.

Since I was already within singleLeg loop, all I needed to call was the buckets object within the loop rather than trying to access the buckets from the rootpath.

Big shoutout to @Fenio who advised the best approaches in Accessing jsonpath elements with nested objects

Mihir
  • 491
  • 5
  • 21
  • Congratulations :) – Fenio Jul 31 '19 at 11:22
  • Thanks very much @Fenio, your help has been invaluable in resolving this. I shall refactor a lot of my tests based on the approaches you have mentioned. They are surely lot more cleaner, easier to maintain and understandable than the for loops. – Mihir Jul 31 '19 at 11:36
  • I also advise looking into `JsonPath` library. Rest Assured uses this one to parse JSON :) – Fenio Jul 31 '19 at 11:37
  • @Fenio : Can you help me please? -https://stackoverflow.com/questions/57374854/how-to-run-a-testng-class-file-with-many-test-methods-multiple-times-in-a-loop – Mihir Aug 06 '19 at 11:21