You may search this topic over the internet but not quite sure “How many methods, symbols you can use in the query and which are they?“. This post is for you, it provides a logical approach to help you investigate by yourself.
Start with a very simple json:
{ "firstName": "John", "lastName": "doe", "age": 26, "address": { "streetAddress": "naist street", "city": "Nara", "postalCode": "630-0192" }, "phoneNumbers": [ { "type": "iPhone", "number": "0123-4567-8888" }, { "type": "home", "number": "0123-4567-8910" } ] }
To get data from json, the syntax of Rest-Assured is:
JsonPath.from(response).get("address.postalCode");
JsonPath.from(response)
–> create JsonPath object from String.- Parameter for
get()
method will be the query. In this case, it’saddress.postalCode
Nội dung bài viết
I. Rules
First, you have to know how groovy converts the json to object:
{}
in json –> a Map[]
in json –> a List
Second, you can use all the List’s methods if you’re dealing with a json array []
, and you can use all the Map’s methods for a json object {}.
Map and List in Groovy are inherited from Java, then you can use both set of methods.
- For
{}
, methods can be applied are: List java, List groovy - For
[]
, methods can be applied are: Map java, Map groovy
II. Map’s Methods
- Get all keys of the json
//method keySet() from Map java Set<?> keys = JsonPath.from(response).get("keySet()"); System.out.println(keys); //[firstName, lastName, age, address, phoneNumbers]
- Check contains key “firstName”
//method containsKey(Object key) from Map java boolean check = JsonPath.from(response).get("containsKey('firstName')"); System.out.println(check); //true
- Check size of the Map
//method size() from Map java int size = JsonPath.from(response).get("size()"); System.out.println(size); //5
- How many key-value pair that having value=26.
//method count(Closure) from Map groovy int value = JsonPath.from(response).get("count{k,v -> v==26}"); System.out.println(value); //1
- Sort the Map by key
//method toSorted() from Map groovy Map<String, ?> newJson = JsonPath.from(response).get("toSorted()"); newJson.forEach((k,v) -> System.out.println(k +":" +v)); //address:{streetAddress=naist street, city=Nara, postalCode=630-0192} //age:26 //firstName:John //lastName:doe //phoneNumbers:[{type=iPhone, number=0123-4567-8888}, {type=home, number=0123-4567-8910}]
III. List’s methods
- Get element by index
//method get(int index) from List Java String firstPhone = JsonPath.from(response).get("phoneNumbers.number.get(0)"); System.out.println(firstPhone); //0123-4567-8888
- Find a first element by condition
//method find(Closure) from List groovy (inherite from Collection groovy) String phone = JsonPath.from(response).get("phoneNumbers.find {it.type == 'iPhone'}.number"); System.out.println(phone); //0123-4567-8888
- Find all elements by condition
//method findAll(Closure) from List groovy List<String> phone = JsonPath.from(response).get("phoneNumbers.findAll {it.type == 'iPhone'}.number"); System.out.println(phone); //[0123-4567-8910]
- Only get the list with unique values
//method toUnique() from List groovy List<String> phone = JsonPath.from(response).get("phoneNumbers.number.toUnique()"); System.out.println(phone); //[0123-4567-8888, 0123-4567-8910]
You can find many other examples here: https://cheatography.com/giang-nd2508/cheat-sheets/rest-assured-extract-value-by-gpath/
IV. Summary
Above examples are just some common cases when dealing with json. There are still a lot of methods that you ultilize to satisfy your needs. Hope this article reveals the mysterious of GPath groovy. And don’t forget to leave a like for this post. Thanks