A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/javascript-json/ below:

JavaScript JSON - GeeksforGeeks

JavaScript JSON

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:

Converting JSON Text to a JavaScript Object

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);

Output
{"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}`);

Output
Name: Mohit, Age: 30
Advantages of JSON Best Practices for Working with JSON in JavaScript Conclusion

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