Last Updated : 11 Jul, 2025
JSON (JavaScript Object Notation) is a lightweight data format for storing and exchanging data. It is widely used to send data between a server and a client. JSON is simple, language-independent, and easy to understand.
Here is an example of JSON that defines an employee object containing an array of three employee records:
JavaScript
{
"employees": [
{ "firstName": "Amit", "lastName": "Kumar" },
{ "firstName": "Jay", "lastName": "Sharma" },
{ "firstName": "Mohit", "lastName": "Patel" }
]
}
JSON Syntax
JSON syntax is derived from JavaScript objects. Here are the rules:
You can use the JSON.parse() method to convert a JSON string into a JavaScript object.
JavaScript
const jsonStr = `{
"emp": [
{ "firstName": "Amit", "lastName": "Kumar" },
{ "firstName": "Jay", "lastName": "Sharma" },
{ "firstName": "Mohit", "lastName": "Patel" }
]
}`;
const obj = JSON.parse(jsonStr);
console.log(obj.emp[1].firstName);
Displaying JSON Data in HTML HTML
<p id="demo"></p>
<script>
const s = `{
"emp": [
{ "firstName": "Amit", "lastName": "Kumar" },
{ "firstName": "Jay", "lastName": "Sharma" },
{ "firstName": "Mohit", "lastName": "Patel" }
]
}`;
const obj = JSON.parse(s);
document.getElementById("demo").innerHTML =
obj.emp[1].firstName + " " + obj.emp[1].lastName;
</script>
Converting a JavaScript Object to JSON
To send data to a server, you need to convert a JavaScript object to a JSON string using JSON.stringify().
JavaScript
const obj = { fname: "Mohit", lname: "Kumar", age: 30 };
const jsonStr = JSON.stringify(obj);
console.log(jsonStr);
{"fname":"Mohit","lname":"Kumar","age":30}JSON Objects
JSON objects are collections of key/value pairs enclosed in curly braces {}.
JavaScript
{
"fName": "Mohit",
"lName": "Kumar",
"age": 30
}
In JavaScript, you can access the data as follows:
JavaScript
const obj = { fName: "Mohit", lName: "Kumar", age: 30 };
console.log(obj.fName);
JSON Arrays
JSON arrays are collections of values enclosed in square brackets [].
{
"hobbies": ["reading", "coding", "traveling"]
}
You can access the data in JavaScript like this:
JavaScript
const obj = { hobbies: ["reading", "coding", "traveling"] };
console.log(obj.hobbies[1]);
JSON in APIs JavaScript JSON
JSON is commonly used in APIs to exchange data between a client and server. In a typical web application, the server sends data to the client (frontend) as JSON.
Server Side:
Client Side:
Here’s a JSON string received from the server
const jsonString = '{"name":"Mohit", "age":30}';
It has an object with the properties
To access its properties, we need to parse it into a JavaScript object:
JavaScript
const jsonS = '{"name":"Mohit", "age":30}';
const obj = JSON.parse(jsonS);
let name = obj.name;
let age = obj.age;
console.log(`Name: ${name}, Age: ${age}`);
Name: Mohit, Age: 30Advantages of JSON
JSON.stringify()
carefully: When working with large objects, avoid stringifying large data structures unnecessarily, as it can be resource-intensive.JSON is an essential data format in modern web development. Its simplicity and widespread support make it a great choice for exchanging data between a client and server. By understanding how to parse, manipulate, and stringify JSON in JavaScript, you can effectively handle data in web applications.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4