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 itMyClassInfo
and parse your Json to aList<MyClassInfo>
. YourMyClassInfo
would look like this:Now you can easily parse it using Json Jackson library: ForMaps:
And for
MyClassInfo
classNow you can just go through your list and extract from each
map
orMyClassInfo
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 withJsonUtils
class (just forMyClassInfo
class:Note that in this case you won't have to instantiate and configure
ObjectMapper
instance asreadObjectFromJsonString
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 forJsonUtils
class is here