- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is JSON parsing in Rest Assured?
We can parse JSON Response with Rest Assured. To parse a JSON body, we shall use the JSONPath class and utilize the methods of this class to obtain the value of a specific attribute.
We shall first send a GET request via Postman on a mock API URL and observe the Response body.
Example
Code Implementation
import org.testng.annotations.Test; import static io.restassured.RestAssured.*; import io.restassured.RestAssured; import io.restassured.path.json.JsonPath; import io.restassured.response.Response; import io.restassured.response.ResponseBody; import io.restassured.specification.RequestSpecification; public class NewTest { @Test void responseParse() { //base URI with Rest Assured class RestAssured.baseURI = "https://run.mocky.io/v3"; //input details RequestSpecification h = RestAssured.given(); //get response Response res = h.get("/a1b7b64c-0204-409a-aa0c-e8314a5ddabf"); //Response body ResponseBody b = res.getBody(); //convert response body to string String responseBody = b.asString(); //JSON Representation from Response Body JsonPath jsnPath = res.jsonPath(); //Get value of Location Key String s = jsnPath.get("student"); System.out.println("Course name: " + s); String std = jsnPath.get("standard"); System.out.println("Standard: " + std); String m = jsnPath.get("marks"); System.out.println("Marks: " + m); } }
Output
- Related Articles
- Validate JSON Schema in Rest Assured.
- What is Rest Assured?
- How to handle static JSON in Rest Assured?
- What is XmlPath in Rest Assured?
- How to verify JSON response headers in Rest Assured?
- How to get a JSON field in a nested JSON using Rest Assured?
- How to get a JSON array field in a nested JSON using Rest Assured?
- How to verify a JSON response body using Assertions in Rest Assured?
- How to extract the whole JSON response as a string in Rest Assured?
- How to get JSON fields(nodes) based on conditions using Rest Assured?
- Explain how to get the size of a JSON array response in Rest Assured.
- How to iterate through and access the JSON array elements using Rest Assured?
- How to get the size of an array within a nested JSON in Rest Assured?
- Explain DELETE request in Rest Assured.
- Explain PUT request in Rest Assured.

Advertisements