Make Selenium Easy

REST Assured Tutorial 13 – How to write API Response in a JSON File

Last updated on January 30th, 2021 at 01:07 am

Introduction

As a part of the End to End REST Assured Tutorial, in this post, We will learn to write API response to a JSON file in Rest Assured.

We may require to store API JSON responses in to JSON files for future reference as a test result. It may also required to pass stored JSON response as an input to another APIs. We can also store JSON response to extract values in future.

Required Dependecies

I have used Rest Assured and TestNG dependecies here. TestNG dependecies we requires to use “Files” class from Google Common dependecies. TestNG dependecy has transitive dependecies of Guava. So here TestNG can be used for both as Test framework and File handling utils.

Writing response into a JSON file

Rest Assured provides below methods to get a response as:-

  1. As byte array :- asByteArray()
  2. As input stream :- asInputStream()
  3. As string :- asString()

All the above methods output can be used to write into a JSON file.

Example program

Output

Refresh the project folder if you are using Eclipse. You should see three files as below:-

Open a file you should see it has stored response as below:-

You may be thinking about which way is more optimal if you want to write into a file. If you notice that we get a response as String or inputStream, we just need to convert them to Byte array before writing into a file. In the third way, we are directly getting Byte Array. Hence the third way is more optimal.

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

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

6 thoughts on “REST Assured Tutorial 13 – How to write API Response in a JSON File

  1. Just to mention: We need to have “json-schema-validator” dependency in order to access Files.write() method from “com.google.common.io” package. Otherwise, we need to add “guava” dependency explicitly.

    1. Yes, that is correct that to use “Files” from Google Common we need “guava” dependency. Since I have TestNG in my project pom.xml and “guava” is a transitive dependency of TestNG.

  2. This is the best thing about your tutorial, you tell concepts like books that too in a noticeably short and precise manner.

    Thanks, keep up the excellent work. Will use asByteArray always

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

REST Assured Tutorial 13 – How to write API Response in a JSON File

by Amod Mahajan time to read: 3 min
6
%d bloggers like this: