0

I have following JSON response anonymous body and I need to parse nested arrays dynamically to retrieve a key's value based on a condition by using find or findAll in the groovy's closures

[
{
  "children": [
    {
      "attr": {
        "reportId": "1",
        "reportShortName": "ABC",
        "description": "test,
      }
    },
   {
     "attr": {
       "reportId": "2",
       "reportShortName": "XYZ",
       "description": "test",
      }
   }
}
]

I've tried the following ways and had no luck to retrieve the reportId key's value from the JSON response

package com.src.test.api;
import static io.restassured.RestAssured.given;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;

public class GetReportId {
   public void getReportId(String reportName) throws Exception {
    String searchReports = "http://localhost:8080/reports";
    Response resp=given().request().when().get(searchReports).then().extract().response();
    JsonPath jsonPath = new JsonPath(resp.asString());

    String reportId1 =jsonPath.get("$.find{it.children.contains(restAssuredJsonRootObject.$.children.find{it.attr.reportShortName == 'ABC'})}.attr.reportId");
    String reportId2 = jsonPath.get("$.find{it.children.attr.reportShortName.contains(restAssuredJsonRootObject.$.children.find{it.attr.reportShortName.equals('XYZ')}.attr.reportShortName)}.attr.reportId");
    System.out.println("ReportId: " + reportId1);
  }
}

There could be multiple JSON objects in the parent anonymous array and need to make use of find or findAll within the groovy closures to get the reportId

Need to get the reportId, but seems that something is wrong. Any help would be appreciated.

Himanshu Bansal
  • 2,003
  • 1
  • 23
  • 46
Raj
  • 31
  • 1
  • 7
  • Any reason you cannot use Groovy's own JSON tools? `new JsonSlurper().parseText(jsonText)*.children*.attr*.findAll{it.reportShortName=='ABC'}*.reportId.flatten()` – hsan Jun 06 '19 at 13:48
  • Thanks! Not really as we are trying to stick to REST-Assured libraries as per the framework – Raj Jun 25 '19 at 10:56

1 Answers1

0

Assuming you want all the reportIds

List<String> reportIds = jsonPath.get("children.flatten().attr.reportId");

will give you what you want, even it the parent anonymous array has multiple entries.

I tested with the following JSON

[
  {
    "children": [
      {
        "attr": {
          "reportId": "1",
          "reportShortName": "ABC",
          "description": "test"
        }
      },
      {
        "attr": {
          "reportId": "2",
          "reportShortName": "XYZ",
          "description": "test"
        }
      }
    ]
  },
  {
    "children": [
      {
        "attr": {
          "reportId": "3",
          "reportShortName": "DEF",
          "description": "test"
        }
      },
      {
        "attr": {
          "reportId": "4",
          "reportShortName": "IJK",
          "description": "test"
        }
      }
    ]
  }
]

and it gives me ["1", "2", "3", "4"] i.e. reportIds from all the children

If you know the index of the reportId you're looking for then you can use it like so:

String reportId = jsonPath.get("children.flatten().attr.reportId[0]");

If you're looking for the reportId of a particular report you can do that too:

String reportId = jsonPath.get("children.flatten().attr.find{it.reportShortName == 'ABC'}.reportId")

will give you "1".

Note: The type of the variable you assign the results to are important for type inference and casting. For example, you CANNOT do:

String [] reportIds = jsonPath.get("children.flatten().attr.reportId");

or

int reportId = jsonPath.get("children.flatten().attr.reportId[0]");

Both those things will throw a ClassCastException.

riyasvaliya
  • 745
  • 6
  • 8