'How to validate the nested api objects in Playwright spi testing
I want to validate the nested api objects. I want to check if the value of 'id' inside the 'data' object is "1"
{
page: 1,
per_page: 6,
total: 12,
total_pages: 2,
data: [
{
id: 1,
email: '[email protected]',
first_name: 'George',
last_name: 'Bluth',
avatar: 'https://reqres.in/img/faces/1-image.jpg'
}
]
}
How can I achieve this?
Solution 1:[1]
First you need to convert your json to a valid one.
And after in java you can do something like this.
public class Validation {
public static void main(String[] args) throws IOException {
String jsonAnswer = "{'page': 1,'per_page': 6,'total': 12,'total_pages': 2,'data':" +
" [{'id': 1,'email': '[email protected]','first_name': 'George','last_name':" +
" 'Bluth','avatar': 'https://reqres.in/img/faces/1-image.jpg'}]}";
JsonObject json = JsonParser.parseString(jsonAnswer).getAsJsonObject();
int id = json.get("data")
.getAsJsonArray().get(0)
.getAsJsonObject().get("id").getAsInt();
if(1 == id){
System.out.println("Validation passed!");
}else{
System.out.println("Validation failed!");
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | MeT |