Introduction💁
REST Assured is a library in Java. It provides a domain-specific language for writing. Also, provide maintainable tests for RESTful APIs. Rest Assured is utilized to test the REST APIs. Java libraries act on the Rest web services like a headless client. The Rest Assured-based libraries are also capable of validating the server's HTTP answers.

In this blog, we will be learning about how to fetch values from JSON objects in REST assured.
JSON Objects In REST Assured ✨
In this article, we will discuss methods to extract values from nested and normal JSON objects. To achieve this, we will be using the “jsonnode-jackson” library present in Java. We need to import the following dependencies in our code to utilize the jackson package in Java.
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.0</version>
</dependency>
Let us first discuss how to fetch the values from a normal JSON file.
Fetching Value From JSON Objects
Let us assume we have the following JSON file,
{
"Company": "Coding Ninjas",
"Location": "Gurugram, India",
"Website": "https://www.codingninjas.com",
"Founded": 2016
}
Now the task at hand is to fetch the values from the above-created JSON file. We will need to achieve the following or similar output,
Company: Coding Ninjas
Location: Gurugram, India
Website: https://www.codingninjas.com
Founded: 2016
We can use the following approach to achieve a similar output.
Code 📝
package JsonCN;
import org.testng.annotations.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
public class fetchJSONValue{
@Test
public void fetchJSONValue() throws JsonMappingException, JsonProcessingException{
String obJSON= "{\r\n" +
" \"Company\": \"Coding Ninjas\",\r\n" +
" \"Location\": \"Gurugram, India\",\r\n" +
" \"Website\": \"https://www.codingninjas.com\",\r\n" +
" \"Founded\": 2016\r\n" +
"}";
// Creating an instance of the
// ObjectMapper class to fetch the values
ObjectMapper obMap = new ObjectMapper();
// fetching the tree representation of
//the JSON using ObjectMapper class
JsonNode jsonOb = obMap.readTree(obJSON);
// Fetching value of company name as a string
String comp = jsonOb.get("Company").asText();
// Fetching value of company location as a string
String loc = jsonOb.get("Location").asText();
// Fetching value of company website as a string
String web = jsonOb.get("Website").asText();
// Fetching value of founded
long founded = jsonOb.get("Founded").asLong();
System.out.println("Company: "+comp);
System.out.println("Location: "+loc);
System.out.println("Website: "+web);
System.out.println("Founded: "+founded);
}
}
In the above code, we have created an instance of the ObjectMapper class to map the JSON object.
You can also observe we have used the asText() function to convert the fetched values to text and the asLong() function to convert the values into a long data type.
Output
