Response JSON Example:
{
"search": {
"street": "31, Trevor Street",
"state": "AL",
"postal_code": "67901",
"phone": "6574727441",
"name": "John Corp",
"country": "US",
"city": "Alabama"
},
"results": {
"speedylocal.com": {
"status": "no-result"
},
"chamberofcommerce.com": {
"status": "complete",
"data": {
"site_logo": "http://cdn.verifymybiz.com/",
"phones": [
"271893712"
],
"name": "Sample",
"live_link": "https://www.chamberofcommerce.com/",
"full_address": "Testing street",
"country": "US",
"accurate": false
}
},
"facebook.com": {
"status": "in-progress"
},
}
}
Dependency :
POM.XML
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.3.0</version>
<exclusions>
<exclusion>
<artifactId>jcl-over-slf4j</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions></dependency>
Sample.java
I saved the rest API response in Response object from RestAssured.
//Reference : https://github.com/json-path/JsonPath
public String getResponseValueUsingJayway(String objectXpath){
return com.jayway.jsonpath.JsonPath.read
(response.jsonPath().prettify(),objectXpath).toString();}
Test.java
Example 1:
String Result=object.getResponseValueUsingJayway("$.results");
Example 2:
String Result=object.getResponseValueUsingJayway("$.results..status");