Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
📚 Introduction
2.
📂 Introduction To JSON
2.1.
Example of JSON
3.
🔰 Formats of JSON
3.1.
JSON object
3.2.
JSON array
3.3.
Example
4.
🏹 Sending a JSON File as a Payload
5.
Frequently Asked Questions
5.1.
Are REST assured and REST API the same?
5.2.
What is the difference between Postman and REST assured?
5.3.
What is REST API in testing?
5.4.
What is JSON used for?
5.5.
What JSON file means?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

REST Assured – Introduction to JSON and how to send files as Payload

Author soham Medewar
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

📚 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.

REST assured

In this blog, we will learn about JSON file format. Also, we will learn to send files as a payload using REST Assured. 

📂 Introduction To JSON

Introduction to JSON

JSON stands for JavaScript Object Notation. It is very lightweight. It is also language-independent. JSON is used for storing data and interchange where the data is stored in a self-describing manner. JSON is very easy to create, read, and write for humans. That's the reason why JSON is famous over XML.

JSON was first derived from JavaScript, but as of now, it is supported by most programming languages. The name of the JSON file is saved using a “.json” as an extension.

Whenever we wish to create a new resource, for example, when we want to add new student information to a student database, it is necessary to send a POST request with a payload to an API endpoint. JSON is a possible payload format. JSON is, therefore, mostly utilized for data exchange between Web Servers and Clients.

Example of JSON

{
	“fname”: “Ganesh”,
	“lname”: “Medewar”,
	“mail”: “sohammedewar@gmail.com”,
	“age”: 22
}


As we can see in the above example that the data is stored in key-value pair. On the left side, we have the key, and on the right side, we have value. Two key-value pairs are separated using a comma (,).

A key in JSON is always a string. The string must be enclosed in double-quotes. The data type of the value can be a number (with and without decimal), string, boolean, object, array, and null.

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

🔰 Formats of JSON

JSON is found in two formats. One is a JSON object, and another is a JSON array.

JSON object

{
	“language”: “C++”,
	“Difficulty”: “Moderate”,
	“Time to Learn (basic)”: “1 month”
}

JSON array

[
	{
		“State”: “Maharashtra”,
		“Language”: “Marathi”
	},
	{
		“State”: “West Bengal“,
		“Language”: “Bengali”
	},
	{
		“State”: “Andhra Pradesh“,
		“Language”: “Telugu”
	},
	{
		“State”: “Punjab“,
		“Language”: “Punjabi”
	}
]


JSON object starts with open curly bracket "{"and ends with close curly bracket "}". It is basically an unordered data structure. In comparison, a JSON array is an ordered collection of data. JSON array starts with the open square bracket "[" and ends with the close square bracket "]". A JSON array is capable of storing multiple JSON objects. 

Example

{
	“Country”: “India”,
	“Population”: “138 crores”,
	“Capital”: “Delhi”,
	“Major Cities”: [
		{
			“City Name”: “Mumbai”,
			“State”: “Maharashtra”
		},
		{
			“City Name”: “Kolkata”,
			“State”: “West Bengal”
		},
		{
			“City Name”: “Hyderabad”,
			“State”: “Telangana”
		},
		{
			“City Name”: “Banglore”,
			“State”: “Karnataka”
		},
	],
	“Tourist Places”: [
		“Ooty”, “Jaipur”, “Coorg”, “Agra”, “Lonavala”, “Darjeeling”
	]
}


In the above JSON, we have the JSON array of objects named “Major Cities”. We also have a JSON array of strings named “Tourist Places”.

🏹 Sending a JSON File as a Payload

sending json file as payload

First, you need to create a JSON file. Then write payload in it. Name the file as someInfo.json. Place the file in the “src/test/resources” folder.

Now here comes the coding part.

package DifferentWaysOfPassingPayloadToRequest;
 
import java.io.File;
import io.restassured.http.ContentType;
import io.restassured.RestAssured;
import org.testng.annotations.Test;
import org.hamcrest.Matchers;
 
public class FilePassingAsPayload {
	
	@Test
	public void passFile()
	{
		String location = "src/test/resources/Payloads/AuthPayload.json";
		/* File instance */
		File jsonDataPayload = new File(location);
		
		/* given */
		RestAssured
		    .given()
				.baseUri("https://restful-app.netlify.com/auth")
				.contentType(ContentType.JSON)
				.body(jsonDataPayload)
		/* when */
			.when()
				.post()
				/* then */
			.then()
				.assertThat()
				.statusCode(200)
				.body("token", Matchers.notNullValue())
				.body("token.length()", Matchers.is(15))
				.body("token", Matchers.matchesRegex("^[a-z0-9]+$"));
	}
}

 

You can also check about Java Tokens here.

Must Read Array of Objects in Java

Frequently Asked Questions

Are REST assured and REST API the same?

A Java library called REST Assured is used to test RESTful APIs. It is frequently used to test web applications that handle JSON and XML. Additionally, it supports all RESTful methods, including PUT, GET, PATCH, POST, and DELETE.

What is the difference between Postman and REST assured?

Code reusability: Since the Java client is REST-assured, it is possible. It can't be done in Postman. Creating a data-driven framework: For each collection, we can only offer one data file to the Postman automation runner. However, there is no restriction for this with REST-assured.

What is REST API in testing?

REST API sometimes referred to as RESTful API, is an API that complies with REST's restrictions and allows communication with RESTful web services. It is an open-source automation method for testing RESTful APIs for web apps. It is frequently used to test web applications that employ JSON and XML.

What is JSON used for?

It uses text to represent scalar values, arrays, and object literals in JavaScript. JSON is simple to parse and generate using software, and it is also simple to read and write. It is frequently used to serialize and exchange structured data across a network, usually between a server and web applications.

What JSON file means?

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. 

Conclusion

In this article, we have learned about the JSON file and its format. Also, we have learned to send a JSON file as a payload in REST Assured.

That's it from the article. I hope you all like it.

Also, you can check our courses and test series for your interview preparations: Coding Ninja Test SeriesCoding QuestionsInterview Preparation ResourcesCoding ContestsProduct-based Company Course, Data Science & ML courseMaster Data Analytics, and ML for beginners. You can also refer to other IT subjects like compiler design and software engineering

Happy Learning!

Live masterclass