Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • About Us
  • Toggle search form

REST Assured Tutorial 65 – How to parse a JSON Object response to a Java Map in Rest Assured?

Posted on 03/21/2025 By admin

As a part of the End to End REST Assured Tutorial, in this post, we will learn to parse a JSON Object response to a Java Map in Rest Assured.

It is not always necessary to convert the response to a POJO class instance to fetch values. We can cast or convert a JSON Object response into a Java Map directly. It is useful if you do not use Pojo classes.

We are using the below version of Rest Assured:-

  io.rest-assured rest-assured 4.3.3 test

Once the response is returned and if it is a JSON object then we can parse them as a Map. There is a method called as() which takes a TypeRef reference to support classes with generics. For example – A JSON Object can be represented as a Map. To cast JSON Object response in such type we need to use as() method with TypeRef. TypeRef is an abstract class that is used to specify generic type information when de-serializing a response.

package RestAssuredConcepts; import java.util.Map; import io.restassured.RestAssured;
import io.restassured.common.mapper.TypeRef;
import io.restassured.http.ContentType; public class ParseJsonObjectResponseToMap { public static void main(String[] args) { Map responseBody = null; responseBody = RestAssured .given() .baseUri("https://restful-booker.herokuapp.com/") .basePath("booking") .contentType(ContentType.JSON) .body("{\r\n" + " \"firstname\" : \"Jim\",\r\n" + " \"lastname\" : \"Brown\",\r\n" + " \"totalprice\" : 111,\r\n" + " \"depositpaid\" : true,\r\n" + " \"bookingdates\" : {\r\n" + " \"checkin\" : \"2018-01-01\",\r\n" + " \"checkout\" : \"2019-01-01\"\r\n" + " },\r\n" + " \"additionalneeds\" : \"Breakfast\"\r\n" + "}") .when() .post() .then() .extract() .body() // Extract response as Map .as(new TypeRef>() {}); // To print booking id System.out.println("Booking id is : "+responseBody.get("bookingid")); // If we do not use below annotation then also no problem. As we are directly casting without checking // so I have used it to surpass warning. @SuppressWarnings("unchecked") // Since "booking" key holds another JSON Object to parsing it again as Map. Map bookingDetails = (Map)responseBody.get("booking"); System.out.println("First name is "+ bookingDetails.get("firstname")); } } 
Booking id is : 11
First name is Jim

You can download/clone the above sample project from here.

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: Git Tutorial 8 – Git Push – Upload Committed Local Repository Changes To Remote Repository Branch
Next Post: nodejs

Related Posts

Difference Between Constructor and Method in Java Uncategorized
How to Verify if Options in Dropdown are Sorted as Expected In Selenium WebDriver – Java | Make Selenium Easy Uncategorized
TestNG Tutorials 50: DataProvider in TestNG – Understand Basics of DataProvider & How It Works | Make Selenium Easy Uncategorized
Make Selenium Easy – Page 27 Uncategorized
image – Make Selenium Easy Uncategorized
AvailableGroups – Make Selenium Easy Uncategorized

Recent Posts

  • How to Set Up Selenium WebDriver with Python Step by Step
  • How to Set Up Selenium WebDriver with Java from Scratch
  • What Is Selenium and Why It Is the Most Popular Test Automation Framework
  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark