Home > Back-end >  Extract a value in a json response with a variable
Extract a value in a json response with a variable

Time:04-20

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

CodePudding user response:

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 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

CodePudding user response:

Thank you for your quick answer, i used the lists and i was able to get what i wanted.

List variable = response.body().jsonPath().get("variable");
List id = response.body().jsonPath().get("id");
        
int value = 0;
for (int i= 0; i <variable.size(); i  ){
            
    if (variable.get(i).equals(87)){
          value = i; 
    }
}
System.out.println(id.get(value));
  • Related