The Wayback Machine - https://web.archive.org/web/20241229220815/https://www.geeksforgeeks.org/json/
Open In App

JSON Tutorial

Last Updated : 13 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

JSON (JavaScript Object Notation) and XML (eXtensible Markup Language) are popular formats for data representation. JSON (JavaScript Object Notation) is a lightweight, text-based data format used for representing structured data.

  • JSON is a lightweight data exchange format that is based on Key-Value pairs.
  • JSON keys are strings, and the values can be any supported type (strings, numbers, objects, arrays, true, false, or null).
  • JSON was inspired by JavaScript's object literal notation and can be used with JavaScript, Python, Java, C#, and PHP.
  • Compared to XML, JSON is more human and machine-readable and requires less bandwidth.

JSON Data Flow: From Server to Client

JSON-Data-FLow
JSON Data Flow: From Server to Client - JSON Tutorial

In a typical web application, the server sends data to the client (frontend) as JSON.

Server Side:

Client Side:

  • The JSON string is received as part of an API response (e.g., via an HTTP GET request).
  • The client parses this string back into a JavaScript object using JSON.parse().
  • The parsed object is then used in the frontend code.

Here’s a JSON string received from the server

const jsonString = '{"name":"Mohit", "age":30}';

It has an object with the properties

  • name: "Mohit"
  • age: 30

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

Key Features of JSON

  • Text-based: JSON is a simple text format, making it lightweight and easy to transmit.
  • Human-readable: It uses key-value pairs, making the structure easy to understand.
  • Language-independent: While it is derived from JavaScript, JSON is supported by many programming languages including Python, Java, PHP, and more.
  • Data structure: It represents data as objects, arrays, strings, numbers, booleans, and null.

JSON Structure

The basic structure of JSON consists of two primary components:

  • Objects: These are enclosed in curly braces {} and contain key-value pairs.
  • Arrays: Arrays are ordered lists enclosed in square brackets [].

JSON Object

{
    "name": "Mohit Kumar",
    "age": 30,
    "isStudent": false
}

JSON Array

{
    "fruits": ["apple", "banana", "cherry"]
}

Work with JSON in JavaScript

In JavaScript, you can easily parse JSON data into a JavaScript object and vice versa using built-in methods like JSON.parse() and JSON.stringify().

Parse JSON to Object

JavaScript
let jsonS = '{"name": "Mohit", "age": 30}';
let jsonObj = JSON.parse(jsonS);

console.log(jsonObj.name);

Output
Mohit

Convert Object to JSON

JavaScript
let obj = {name: "Mohit", age: 30};
let jsonS= JSON.stringify(obj);

console.log(jsonS);

Output
{"name":"Mohit","age":30}

Work with JSON in Python

In Python, you can use the json module to handle JSON data. The process involves loading JSON data into Python dictionaries and vice versa.

Parse JSON to Dictionary

import json

json_str = '{"name": "Mohit", "age": 30}'
data = json.loads(json_str)

print(data["name"])  # Output: Mohit

Convert Dictionary to JSON

import json

data = {"name": "Mohit", "age": 30}
json_str = json.dumps(data)

print(json_str) # Output: '{"name": "Mohit", "age": 30}'

Applications of JSON

  • APIs: JSON is the most commonly used format for API responses due to its lightweight nature.
  • Configuration Files: Many software systems use JSON files for storing configuration data.
  • Databases: Some NoSQL databases, like MongoDB, store data in JSON-like formats.
  • Data Transfer: JSON is widely used for transferring data between servers and clients, especially in web development.
JSON Tutorial

JSON Basics

JSON SetUp

Differences

JSON Methods

Miscellaneous

Important Questions

JSON Interview Questions

Useful JSON Tools

JSON Tutorial - FAQ's

What is the full-form JSON?

"JSON" stands for " JavaScript Object Notation " and is a lightweight data format used for easy exchange of data between different systems.

What is JSON used for?

JSON is used for data interchange between servers and web applications, configuration files, and storing structured data.

What is the basic structure of JSON?

JSON structure is made of key-value pairs within objects {} and ordered lists of values within arrays [].

What are the types of JSON?

String, Number, Object, Array Boolean, Null

Is JSON Slowing Down Our Apps?

In some case, especially for large data sets. JSON is easy to read but larger and requires more processing than some alternatives

Why is JSON preferred over XML?

JSON is more lightweight, easier to read and write, and faster to parse than XML.

What are the alternative of JSON?

The best choice depends on your needs: data size, readability, and development time. Here are some alternatives of JSON:

  • Protocol Buffers: Smaller, faster format, good for various languages.
  • MessagePack: Efficient for real-time data exchange.
  • Custom Binary Formats: Fastest option, but requires more development effort.

Similar Reads

three90RightbarBannerImg