Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction💁
2.
JSON Objects In REST Assured ✨
3.
Fetching Value From JSON Objects
3.1.
Code 📝
3.2.
Output
4.
Fetching Value From Nested JSON Objects
4.1.
Code📝
4.2.
Output
5.
Frequently Asked Questions
5.1.
What is REST Assured?
5.2.
Is REST assured open-source?
5.3.
What is the difference between REST Assured and REST?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Fetching Values From JSON Objects In REST Assured

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

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.

Introduction

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

Output 1
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Fetching Value From Nested JSON Objects

Let us assume we have the following JSON file,

{ 
 "Company": "Coding Ninjas",
  "Location": "Gurugram, India",
  "Website": "https://www.codingninjas.com",
  "Founded": 2016,
  "SocialMedia": {
    "LinkedIn": "https://in.linkedin.com/company/codingninjas",
    "YouTube": "https://www.youtube.com/channel/UCZJlMUYdbtzQ8tVfLvK1KvQ"
  }
}


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
SocialMedia: 
     https://in.linkedin.com/company/codingninjas
     https://www.youtube.com/channel/UCZJlMUYdbtzQ8tVfLvK1KvQ


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" + 
                "  \"SocialMedia\" :{\r\n" + 
                "    \"LinkedIn\" :\"https://in.linkedin.com/company/codingninjas\",\r\n" + 
                "    \"YouTube\" :\"https://www.youtube.com/channel/UCZJlMUYdbtzQ8tVfLvK1KvQ\",\r\n" +  
                "  }\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
        int founded = jsonOb.get("Founded").asLong();


        // Fetching value of LinkedIn
        int socLink = jsonOb.get("SocialMedia").get("LinkedIn").asText();
        int socYou = jsonOb.get("SocialMedia").get("YouTube").asText();
        
        System.out.println("Company : "+comp);
        System.out.println("Location : "+loc);
        System.out.println("Website : "+web);
        System.out.println("Founded : "+founded); 
        System.out.println("Social Media : ");
        System.out.println("\t" + socLink);
        System.out.println("\t" + socYou); 
    } 
}


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. We have used an extended get function to fetch values for different keys present under “SocialMedia.”

Output

Output 2

 

Now, let's discuss some frequently asked questions associated with the REST Assured.

FAQs

Frequently Asked Questions

What is REST Assured?

REST Assured is a very potent Java library that is used to write and maintain tests for RESTful APIs. 

Is REST assured open-source?

Yes, REST assured is an open-source Java Domain-specific language (DSL) that makes testing REST service simple.

What is the difference between REST Assured and REST?

REST stands for Representation State Transfer. It is a set of protocols laid forward for developing APIs. In contrast, REST Assured is a Java library that is used to write and manage test cases for REST APIs.

Conclusion

This Blog covered all the necessary points associated with fetching values from a JSON object in REST Assured. We discussed how to fetch values from normal as well as nested JSON objects in REST Assured. 
Hey Ninjas! Don’t stop here. Check out Coding Ninjas for Java, more unique courses, and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and excellent Machine Learning and Java problems.

Previous article
Ignore Unknown Properties During Deserialization in Rest Assured
Next article
How To Pass or retrieve Headers In Rest Assured Requests
Live masterclass