Extract a value in a json response with a variable

2k Views Asked by At

I am trying to return the id of a declared variable from a json file: i want my java code to search for the previous variable in the json response and once it is found -> return the corresponding id. I am using RestAssured in java.

Json file response example:

[
 {
  "class" : "test 1",
  "id"    : 5,
  "variable" : 87
  },
 {
  "class" : "test 2",
  "id"    : 7,
  "variable" : 76
  }
]

Now, i do something like that but i have no idea:

Response response = given()
                .config(config)
                .header("accept","application")
                .when()
                .get("requestID")
                .then()
                .extract().response();

String id = String.valueOf(response.
                body().
                jsonPath().
                get("id").toString().contains(variable));

Thank you

3

There are 3 best solutions below

0
On

All you need is just to parse your Json. This could be done very simply. You can parse your given Json to a List<Map> or you can create a simple class lets call it MyClassInfoand parse your Json to a List<MyClassInfo>. Your MyClassInfo would look like this:

public class MyClassInfo {
  @JsonProperty("class")
  private String myClass;
  private Integer id;
  private Integer variable;
  // Add setters and getters here
}

Now you can easily parse it using Json Jackson library: ForMaps:

List<Map<String, Object>> myList;
ObjectMapper = new ObjectMapper();
//Add setters for ObjectMapper configuration here if you want a specific config
try {
  myList = objectMapper.readValue(myJsonString, List.class);
} catch(IOException ioe) {
...
}

And for MyClassInfo class

List<MyClassInfo> myList;
ObjectMapper = new ObjectMapper();
//Add setters for ObjectMapper configuration here if you want a specific config
try {
  myList = objectMapper.readValue(myJsonString, List.class);
} catch(IOException ioe) {
...
}

Now you can just go through your list and extract from each map or MyClassInfo the required id.

Just to simplify it a bit more you can do parsing with JsonUtils class from MgntUtils library written by me. Here is the code with JsonUtils class (just for MyClassInfo class:

    List<MyClassInfo> myList;
    try {
      myList = JsonUtils.readObjectFromJsonString(myJsonString, List.class);
    } catch(IOException ioe) {
    ...
    }

Note that in this case you won't have to instantiate and configure ObjectMapper instance as readObjectFromJsonString is a static method. Anyway if you are interested in using my library you can find maven artifacts here and The library itself with source code and javadoc is on Github here. Javadoc for JsonUtils class is here

2
On

Quick solution here, query by condition with GPath syntax.

int id = response.jsonPath().get("find {it.variable == 87}.id");
0
On

We can use jsonpath to get a list of objects matching our criteria and return a list or map.

Response response = given()
                .config(config)
                .header("accept","application")
                .when()
                .get("requestID")
                .then()
                .extract().response();

Here I'm assuming the variable is class='test 1'. You can give any criteria to match against.

List<Integer> ids = response.jsonpath().get("*[?(@.class=='test 1')]..id");

System.out.println(ids.get(0));

Refer: https://github.com/json-path/JsonPath