JSON (JavaScript Object Notation) is one of the most common formats used to send and receive data through REST APIs. In Postman, JSON can be used to test POST, PUT, and PATCH requests.
JSON stands for JavaScript Object Notation. It is a lightweight text format used for storing and exchanging data.
Example:
{
"name": "Rahul",
"course": "ADCA"
}
REST APIs commonly use JSON because it is easy for both humans and programming languages to read and process.
A server can send JSON to a React Native application, and the application can convert that data into objects that it can use.
A JSON object is enclosed inside curly braces:
{ }
It contains data in the form of key-value pairs.
{
"name": "Amit",
"age": 22
}
A JSON property contains a key and a value separated by a colon.
{
"name": "Amit"
}
Here:
String values are written inside double quotation marks.
{
"name": "Rahul Kumar",
"course": "Python"
}
Both Rahul Kumar and Python are strings.
JSON can also contain numeric values.
{
"age": 21,
"fee": 5000
}
Numbers do not need quotation marks.
JSON supports the Boolean values true and false.
{
"active": true,
"verified": false
}
Boolean values are not written inside quotation marks.
JSON also supports the null value.
{
"middle_name": null
}
Null can represent the absence of a value.
A JSON array is enclosed in square brackets.
[
"HTML",
"CSS",
"JavaScript"
]
Arrays are useful when multiple values need to be represented.
A JSON array can contain multiple objects.
[
{
"id": 1,
"name": "Rahul"
},
{
"id": 2,
"name": "Priya"
}
]
This format is commonly returned by APIs that provide lists of records.
Postman allows us to enter JSON data when testing APIs.
For example, a POST request can send a new student as JSON.
{
"name": "Rahul",
"course": "ADCA"
}
To send JSON data in Postman:
In the Body section, select the raw option.
This provides a text editor where you can directly enter JSON data.
{
"name": "Amit",
"course": "Java"
}
After selecting raw, choose JSON from the format dropdown.
This tells Postman that the request body is JSON data.
JSON
Suppose we want to add a student using a POST request.
{
"name": "Rahul Kumar",
"course": "ADCA",
"mobile": "9876543210",
"fee": 5000
}
This JSON contains four properties.
Multiple JSON properties are separated using commas.
Correct example:
{
"name": "Rahul",
"course": "ADCA",
"fee": 5000
}
A missing comma can make the JSON invalid.
JSON property names and string values use double quotation marks.
Correct:
{
"name": "Rahul"
}
Always use valid JSON syntax when sending data through Postman.
When sending JSON data, the Content-Type header is commonly:
Content-Type: application/json
This tells the server that the request body contains JSON data.
POST requests can use JSON to send new information to an API.
POST
http://localhost/api/students.php
{
"name": "Amit",
"course": "Python"
}
PUT requests can use JSON to send complete updated information for an existing resource.
PUT
http://localhost/api/students.php?id=10
{
"name": "Amit Kumar",
"course": "Java",
"mobile": "9876543210"
}
PATCH requests can use JSON to send only the fields that need to be changed.
PATCH
http://localhost/api/students.php?id=10
{
"course": "React Native"
}
Only the specified field needs to be updated by an API designed for partial updates.
APIs can also return JSON data in their responses.
{
"status": true,
"message": "Student added successfully",
"student_id": 15
}
Postman displays the JSON response after the request is completed.
An API that returns multiple records may send a JSON array.
[
{
"id": 1,
"name": "Rahul",
"course": "ADCA"
},
{
"id": 2,
"name": "Priya",
"course": "Python"
}
]
Before sending a request, check that the JSON is correctly formatted.
Check:
The following JSON is invalid because there is no comma between the properties:
{
"name": "Rahul"
"course": "ADCA"
}
The correct version is:
{
"name": "Rahul",
"course": "ADCA"
}
JSON supports several important data types:
Understanding these types is important when working with API requests and responses.
React Native applications commonly receive JSON data from REST APIs.
REST API
↓
JSON Response
↓
React Native
↓
Display Data
JSON therefore forms an important connection between the backend API and the mobile application.
Method:
POST
URL:
http://localhost/api/students.php
Body → raw → JSON:
{
"name": "Amit Kumar",
"course": "React Native",
"mobile": "9876543210"
}
Click Send to send the JSON data to the API.
The API can return a JSON response such as:
{
"status": true,
"message": "Student added successfully",
"data": {
"id": 15,
"name": "Amit Kumar",
"course": "React Native"
}
}
Postman displays this response so that we can inspect the API result.
JSON is one of the most important data formats used with REST APIs. In Postman, JSON can be entered in the Body section using the raw and JSON options.
Postman
↓
HTTP Request
↓
JSON Data
↓
REST API
↓
JSON Response
↓
React Native App
Question: Which option should normally be selected in Postman's Body section when you want to send JSON data?