Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JSON Introduction
Next article icon

JSON Tutorial

Last Updated : 27 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

JSON (JavaScript Object Notation) is a widely-used, lightweight data format for representing structured data. It is used extensively in APIs, configuration files, and data exchange between servers and clients. JSON (JavaScript Object Notation) and XML (eXtensible Markup Language) are popular formats for data representation.

This comprehensive article covers everything we need to know about JSON, including its structure, syntax, applications, and how to work with it in different programming languages.

What is JSON (JavaScript Object Notation)?

JSON stands for JavaScript Object Notation and is a lightweight, text-based data format designed for easy data exchange. JSON is widely used to transmit data between a server and a client as part of a web API response. It is easy to read and write for humans and machines alike, which makes it a preferred choice for data interchange in web applications.

Key Characteristics 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 Data Flow: From Server to Client

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

JSON vs XML: A Quick Comparison

When it comes to data formats, JSON and XML are the two most common choices. JSON is generally preferred for web applications due to its smaller size, ease of use, and better performance. Here's a quick comparison:

FeatureJSONXML
ReadabilityHuman-readableHuman-readable but more verbose
Data SizeSmaller and more compactLarger due to extra markup
ParsingEasier to parse in most languagesMore complex parsing
SupportBroad support across languagesInitially JavaScript, but now widely supported
Use CasesWeb APIs, configuration files, data transferData storage, document formatting

How JSON Data Flow Works in Web Applications

In a typical web application, JSON (JavaScript Object Notation) is used to transfer data between the server and the client (frontend).

Server Side:

  • Data is stored as a JavaScript object.
  • Before sending the data to the client, it's converted to a JSON string using JSON.stringify().

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:

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

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 [].

Objects in JSON

A JSON object is a collection of key-value pairs enclosed in curly braces {}. The key is always a string, and the value can be a variety of data types, including strings, numbers,arrays and even other objects.

Example:

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

In this example, name, age, and isStudent are keys, and "John Doe", 30, and false are their respective values.

Arrays in JSON

A JSON array is an ordered collection of values enclosed in square brackets []. These values can be of any type, including objects, arrays, or primitive data types.

Example:

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

Here, fruits is a key, and the value is an array containing the elements "apple", "banana", and "cherry".

Key JSON Data Types

JSON supports the following data types:

  • String: A sequence of characters, e.g., "hello".
  • Number: Integer or floating-point numbers, e.g., 10, 3.14.
  • Boolean: A value representing true or false.
  • Array: An ordered list of values.
  • Object: A collection of key-value pairs.
  • Null: A null value indicating the absence of any value.

How to Work with JSON in JavaScript

In JavaScript, we 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

To parse a JSON string into a JavaScript object, use JSON.parse().

Example:

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

console.log(jsonObj.name);

Output

Mohit

Convert Object to JSON

To convert a Javascript object into a JSON string, use JSON.stringify()

Example:

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

console.log(jsonS);

Output

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

How to Work with JSON in Python

Python provides a built-in json module to work with JSON data. We can use the json.loads() method to convert a JSON string to a dictionary and json.dumps() to convert a dictionary to a JSON string.

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 Introduction
  • How to Comment in JSON Files?
  • JSON Data Types
  • JavaScript JSON Objects
  • What is JSON Array?

JSON SetUp

  • How to Format JSON in VSCode
  • Json-Server Setup And Introduction
  • What is the correct JSON content type ?

Differences

  • JSON vs JavaScript Object
  • Difference between JSON and XML
  • What is the difference between YAML and JSON?
  • Difference Between JSON and BSON
  • Difference Between JSON and AJAX

JSON Methods

  • JSON Stringify

Miscellaneous

  • JSON Web Token (JWT)
  • Python JSON
  • What is JSON-Java (org.json)?
  • JSON Schema
  • JSON Interview Questions
  • JavaScript JSON Complete Reference

Important Questions

  • How to Convert Map to JSON in JavaScript ?
  • How to Convert JSON Object to CSV in JavaScript ?
  • How to Convert Blob Data to JSON in JavaScript ?
  • How to Convert Excel to JSON in JavaScript ?

JSON Interview Questions

  • JSON Interview Questions

Useful JSON Tools

  • JSON to CSV Converter
  • JSON Formatter and Validator
  • JSON to XML Converter

Conclusion

JSON is an essential part of modern web development. Whether we’re working with APIs, configuration files, or data storage, understanding JSON and how to work with it in different programming languages is important for any developer. This article provides a detailed explanation of JSON’s structure, syntax, and usage across multiple platforms. By mastering JSON, we can significantly improve the efficiency and scalability of our web applications.


Next Article
JSON Introduction

H

hardiksm73
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • Databases
  • JSON
  • Tutorials
  • Web-Tech Tutorials
  • JavaScript-JSON

Similar Reads

  • JSON Tutorial
    JSON (JavaScript Object Notation) is a widely-used, lightweight data format for representing structured data. It is used extensively in APIs, configuration files, and data exchange between servers and clients. JSON (JavaScript Object Notation) and XML (eXtensible Markup Language) are popular formats
    6 min read
  • JSON Introduction
    JSON (JavaScript Object Notation) is a lightweight, text-based data format used for representing structured data. It is one of the most widely used formats for data interchange between clients and servers in web applications. JSON has replaced XML as the preferred data format due to its simplicity,
    5 min read
  • JSON Full Form
    JSON stands for JavaScript Object Notation. It is a popular data interchange format used in many applications and technology stacks. JSON is easy for both humans and machines to read.It is not tied to any specific programming language and can be combined with C++, Java, and Python.It represents data
    1 min read
  • JSON Data Types
    JSON (JavaScript Object Notation) is the most widely used data format for data interchange on the web. JSON is a lightweight text-based, data-interchange format and it is completely language-independent. JSON Data Types JSON supports mainly 6 data types: StringNumberBooleanNullObjectArrayNote: strin
    2 min read
  • JSON Schema
    JSON Schema is a content specification language used for validating the structure of a JSON data.It helps you specify the objects and what values are valid inside the object's properties. JSON schema is useful in offering clear, human-readable, and machine-readable documentation. Structure of a JSON
    2 min read
  • JavaScript JSON
    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. JSON stands for JavaScript Object Notation.It is a lightweight, text-based data
    5 min read
  • JavaScript JSON stringify() Method
    The JSON.stringify() method in JavaScript is used to convert JavaScript objects into a JSON string. This method takes a JavaScript object as input and returns a JSON-formatted string representing that object. Syntax: JSON.stringify(value, replacer, space);Parameters: value: It is the value that is t
    3 min read
  • What is JSON text ?
    JSON text refers to a lightweight, human-readable format for structuring data using key-value pairs and arrays. It is widely used for data interchange between systems, making it ideal for APIs, configuration files, and real-time communication. In today’s interconnected digital world, data flows seam
    4 min read
  • Interesting facts about JSON
    JSON or JavaScript Object Notation is a lightweight format for transporting and storing data. JSON is used when data is transferred from a server to a web page. This language is also characterized as weakly typed, prototype-based, dynamic, and multi-paradigm. Here are some interesting facts about JS
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences