Reputation: 77
I am getting response from the server in json format by hitting post request using restAssured using java.
Response response = requestSpecification.body(text).post(endpoint);
now i want to fetch sub json object named "freebies". How can i perform this operation because when i am writting :
JSONObject responseJSONObject = new JSONObject(response);
//jsonObject.getJSONObject("freebies").getString("id");
JSONArray list = jsonObject.getJSONArray("freebies");
String freebies = list.getJSONObject(0).getString("id");
for (int i = 0; i < freebies.length(); i++) {
String id = list.getJSONObject(i).getString("id");
System.out.println(id.toString());
String name = list.getJSONObject(i).getString("name");
System.out.println(name.toString());
String packName = list.getJSONObject(i).getString("packName");
System.out.println(packName.toString());
String quota = list.getJSONObject(i).getString("quota");
System.out.println(quota.toString());
I am getting stackoverFlow err.
Please help.
Upvotes: 1
Views: 6665
Reputation: 1249
In RestAssured 4.4.0 the following works:
JSONObject jsonObject = new JSONObject(response.jsonPath().getJsonObject("$"));
Upvotes: 0
Reputation: 29669
ValidatableResponse statusResponse = givenJsonRequest().when()
.get("/field/entity/test").then();
ArrayList<Map<String,?>> jsonAsArrayList = statusResponse.extract()
.jsonPath().get("");
Optional<Map<String,?>> filtered = jsonAsArrayList.stream()
.filter(m -> m.get("fieldName1").equals("Whatever1"))
.filter(m -> m.get("jsonObject").toString().contains("Whatever2"))
.findFirst();
Assert.assertTrue(filtered.isPresent(), "Test expected a result after filtering.");
Upvotes: 0
Reputation: 21
Try to use the line below to translate restassured response body to JSONObject format:
JSONObject responseJSONObject = new JSONObject(response.getBody().asString);
Upvotes: 2