harry
harry

Reputation: 35

Facing issue in extracting value from nested jsonArray

I know a similar question has been posted, however, the answer didn't work for me.

I am new to this RestAssured.I want to get the 'uuid' value if categories equals to 'FUNGI' and feature->features conatains 'VRA'.This is my sample json:

[{
      "uuid":"e223d29b-499b-b58b-995e-654bef1aab03",
      "categories":[
         {
            "uuid":"89d1c022-4453-5f6b-883c-46730d429b2a",
            "name":"FERTI"
         }
      ],
      "minRateSi":0.0
   },
   {
      "uuid":"93015a1b-76ac-2ca3-2bbc-5ae5480962c2",
      "categories":[
         {
            "uuid":"61c951b1-3e47-f0a0-80d8-3d43efa339fb",
            "name":"FUNGI"
         }
      ],
      "minRateSi":0.0,
      "maxRateSi":7.5E-8,
      "features":[
         {
            "id":"9b4ee6b2-ae2d-6c9a-af77-08a4b749031f",
            "vraMinRate":2.0E-8,
            "features":[
               "VRA"
            ]
         },
         {
            "id":"ec0d0f52-dd71-ebb9-0a39-831768fe4490",
            "vraMinRateSi":3.0E-8,
            "features":[
               "VRA"
            ]
         }
      ]
   },
   {
      "uuid":"38290452-4937-4f33-c54d-7f502b84ed99",
      "categories":[
         {
            "uuid":"2c9d8cc0-01bc-899d-6782-cf412e90fd78",
            "name":"FUNGI"
         }
      ],
      "maxRateSi":1.0E-7,
      "features":[
         {
            "id":"9b4ee6b2-ae2d-6c9a-af77-08a4b749031f",
            "vraMinRateSi":6.5E-8
         },
         {
            "cropUuid":"ec0d0f52-dd71-ebb9-0a39-831768fe4490",
            "vraMinRateSi":5.0E-8
         }
      ]}]

Upvotes: 1

Views: 47

Answers (1)

Phill Alexakis
Phill Alexakis

Reputation: 1499

Woo , okay your case was pretty hard to break down, but as far as you know how to use JSONArray and JSONObject classes you will be fine.

First of all let me mention how you should approach this kind of scenarios.


I prefer using an online JSON Formatter like this one simply paste your JSON payload and you will be able to tell whether to use JSONObject or JSONArray.

Please take a note here, and check out how to parse json payloads

This code will work for you, although you need to find out the logic behind it and implement JSON parsing accordingly in your future cases.

//declaring your payload
String myJsonPayload = "[{\r\n" + "      \"uuid\":\"e223d29b-499b-b58b-995e-654bef1aab03\",\r\n" +
 "      \"categories\":[\r\n" + "         {\r\n" +
 "            \"uuid\":\"89d1c022-4453-5f6b-883c-46730d429b2a\",\r\n" +
 "            \"name\":\"FERTI\"\r\n" + "         }\r\n" + "      ],\r\n" +
 "      \"minRateSi\":0.0\r\n" + "   },\r\n" + "   {\r\n" +
 "      \"uuid\":\"93015a1b-76ac-2ca3-2bbc-5ae5480962c2\",\r\n" + "      \"categories\":[\r\n" +
 "         {\r\n" + "            \"uuid\":\"61c951b1-3e47-f0a0-80d8-3d43efa339fb\",\r\n" +
 "            \"name\":\"FUNGI\"\r\n" + "         }\r\n" + "      ],\r\n" +
 "      \"minRateSi\":0.0,\r\n" + "      \"maxRateSi\":7.5E-8,\r\n" + "      \"features\":[\r\n" +
 "         {\r\n" + "            \"id\":\"9b4ee6b2-ae2d-6c9a-af77-08a4b749031f\",\r\n" +
 "            \"vraMinRate\":2.0E-8,\r\n" + "            \"features\":[\r\n" +
 "               \"VRA\"\r\n" + "            ]\r\n" + "         },\r\n" + "         {\r\n" +
 "            \"id\":\"ec0d0f52-dd71-ebb9-0a39-831768fe4490\",\r\n" +
 "            \"vraMinRateSi\":3.0E-8,\r\n" + "            \"features\":[\r\n" +
 "               \"VRA\"\r\n" + "            ]\r\n" + "         }\r\n" + "      ]\r\n" + "   },\r\n" +
 "   {\r\n" + "      \"uuid\":\"38290452-4937-4f33-c54d-7f502b84ed99\",\r\n" +
 "      \"categories\":[\r\n" + "         {\r\n" +
 "            \"uuid\":\"2c9d8cc0-01bc-899d-6782-cf412e90fd78\",\r\n" +
 "            \"name\":\"FUNGI\"\r\n" + "         }\r\n" + "      ],\r\n" +
 "      \"maxRateSi\":1.0E-7,\r\n" + "      \"features\":[\r\n" + "         {\r\n" +
 "            \"id\":\"9b4ee6b2-ae2d-6c9a-af77-08a4b749031f\",\r\n" +
 "            \"vraMinRateSi\":6.5E-8\r\n" + "         },\r\n" + "         {\r\n" +
 "            \"cropUuid\":\"ec0d0f52-dd71-ebb9-0a39-831768fe4490\",\r\n" +
 "            \"vraMinRateSi\":5.0E-8\r\n" + "         }\r\n" + "      ]}]";

JSONArray json = new JSONArray(myJsonPayload);
System.out.println(json);

for (int i = 0; i < json.length(); i++) {
 JSONObject object = (JSONObject) json.get(i);
 // System.out.println(object); // you have each JSONObject { } contained in the
 // Outer JSONArray [ ]
 //Getting both uuid's since you didn't answer which one.
 String uuid = object.getString("uuid");
 System.out.println("\n\n Next Object - Outer UUID: " + uuid);
 JSONArray categories = object.getJSONArray("categories");
 System.out.println(categories);
 if (((JSONObject) categories.get(0)).getString("name").equalsIgnoreCase("fungi")) {
  System.out.println("\nFUNGI found!:\n");
  //Getting the inner UUID
  String uuidOfFungi = ((JSONObject) categories.get(0)).getString("uuid");
  System.out.println("Inner UUID: " + uuidOfFungi);
 } else {
  System.out.println("\nFUNGI NOT FOUND (ABORTING):\n");
 }


 // Getting Features:
 try {
  JSONArray featuresObject = object.getJSONArray("features");

  for (int index = 0; index < featuresObject.length(); index++) {
   try {
    JSONObject features = featuresObject.getJSONObject(index);
    //Getting VRA
    String vraFeatues = features.getJSONArray("features").getString(0);

   } catch (JSONException e) {
    System.out.println("No inner JSONArray in features JSONObject");
   }
  }


 } catch (JSONException e) {
  System.out.println("JSON OBJECT " + i + " HAS NO FEATURES ARRAY");
 }


}


Depending again on your use-case it's up to you how you will get the JSON payload into your program.

Either case, modifications need to be done in order to initialize myJsonPayload accordingly.

Hope it helped!

Upvotes: 1

Related Questions