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
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 itMyClassInfoand parse your Json to aList<MyClassInfo>. YourMyClassInfowould look like this:Now you can easily parse it using Json Jackson library: ForMaps:
And for
MyClassInfoclassNow you can just go through your list and extract from each
maporMyClassInfothe required id.Just to simplify it a bit more you can do parsing with
JsonUtilsclass from MgntUtils library written by me. Here is the code withJsonUtilsclass (just forMyClassInfoclass:Note that in this case you won't have to instantiate and configure
ObjectMapperinstance asreadObjectFromJsonStringis 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 forJsonUtilsclass is here