-1

I am working on updating functional test suites using Cucumber feature file.The issue my output is an array which is not sorted.Index of the object may change.

Array:

    [{

        "id": "12",
        "name": "Something"
    },
    {

        "id": "13",
        "name": "Another Something"
    }
 ]

Here I wanna assert name when Id=13 only.Any help would be appreciated.

Rocky4Ever
  • 828
  • 3
  • 12
  • 35

1 Answers1

0

The Json is list of objects within an Array so you need parse them and validate the each Object. You can do like below,

Code:

JSONArray jsonArray = new JSONArray(JsonAsString);
JSONObject jsonObject;

for (int i = 0; i < jsonArray.length(); i++) {
    jsonObject = new JSONObject(jsonArray.get(i).toString());
    if (jsonObject.get("id").toString().equalsIgnoreCase("13")) {
        System.out.println("Name: " + jsonObject.get("name"));
        //do your thing...
    }
}

Output:

Name: Another Something
Nandan A
  • 2,702
  • 1
  • 12
  • 23