0

I have a nested JSON response. JsonResponse Screenshot

I want to get the the dictionary at 0th location from the list and then get a particular element from it. For e.g. In response, {0} and {1}, I want to get complete {0} as a result. Then from {0}, I want to extract "Id" value only.
I don't want to use the JsonPath.read(JsonResponse String, JSON Path) everytime. So looking for some simple and better alternative.

How to convert JSON response to Java List. Below is the response.

Response resp = given().header("Authorization", "Bearer "+"dwded").
                accept(ContentType.JSON).
                when().
                get("https://example.com");      
                return resp;
D Bhatnagar
  • 133
  • 1
  • 1
  • 9
  • I'd write it in the editor.swagger.io and then have the editor generate the java code for you. – dskow Dec 15 '17 at 16:31
  • If you have a Java class which represents your response you could make use of Response#readEntity and then just access it like any other Java object – Matthew Clark Dec 15 '17 at 16:40
  • @Matthew Clark how to do that. Sorry I didn't get it. I am using Rest assured. Can it be used here? – D Bhatnagar Dec 15 '17 at 18:55

2 Answers2

0

For testing a web-API or REST endpoint, I would recommend Karate.

So it becomes simple:

* def id = response[0].Id
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
0

In the swagger editor pet example.

  responses:
    200:
      description: "successful operation"
      schema:
        type: "array"
        items:
          $ref: "#/definitions/Pet"

A model is generated from the

  Pet:
    type: "object"
    properties:
      name:
        type: "string"
        example: "doggie"

This generated a java class

public class Pet   {

  @JsonProperty("name")
  private String name = null;

The api shows a REST that returns an entity that can be shown as an array of json objects

    ResponseEntity<List<Pet>> findPetsByStatus( @NotNull@ApiParam(value = "Status values that need to be considered for filter", required = true, allowableValues = "available, pending, sold") @RequestParam(value = "status", required = true) List<String> status);
dskow
  • 924
  • 6
  • 9