JSON โ short for JavaScript Object Notation โ is the most widely used data format on the internet today. Whether you are calling a REST API, storing configuration, or sending data between a frontend and a backend, JSON is almost certainly involved. In this guide, you will learn exactly what JSON is, how it works, and how to use it confidently as a developer.
What is JSON?
JSON is a lightweight, text-based format for representing structured data. It was derived from JavaScript object syntax, but it is language-independent โ meaning every major programming language (Python, Java, C#, PHP, Go, Ruby, and more) has built-in support for reading and writing JSON.
JSON was created by Douglas Crockford in the early 2000s as a simpler alternative to XML for transmitting data between a server and a web application. Today it is the dominant data format for APIs, configuration files, databases like MongoDB, and data storage.
JSON Syntax Rules
JSON has six simple rules that you must follow exactly:
- Data is represented in name/value pairs (like a dictionary or map)
- Data is separated by commas
- Curly braces
{}hold objects - Square brackets
[]hold arrays - All keys (names) must be strings wrapped in double quotes
- String values must also use double quotes (single quotes are not valid JSON)
JSON Data Types
JSON supports exactly six data types:
- String โ
"Hello, World!"โ always in double quotes - Number โ
42or3.14โ integers and decimals, no quotes - Boolean โ
trueorfalseโ lowercase, no quotes - Null โ
nullโ represents the absence of a value - Object โ
{"key": "value"}โ key-value pairs inside curly braces - Array โ
[1, 2, 3]โ ordered list inside square brackets
A Simple JSON Example
Here is a JSON object representing a user profile:
{
"id": 101,
"name": "Arjun Sharma",
"email": "arjun@example.com",
"age": 28,
"isVerified": true,
"address": {
"city": "Bengaluru",
"state": "Karnataka",
"pincode": "560001"
},
"skills": ["JavaScript", "Python", "React", "Node.js"],
"profilePicture": null
}
Notice how this single JSON object contains a string, number, boolean, nested object, array, and null โ demonstrating all six data types at once.
JSON Objects vs JSON Arrays
A JSON object is an unordered collection of key-value pairs. Use it when you have named properties, like a user, a product, or a configuration setting.
A JSON array is an ordered list of values. Use it when you have a collection of similar items, like a list of users or a series of results from an API.
// Array of users
[
{"id": 1, "name": "Arjun"},
{"id": 2, "name": "Priya"},
{"id": 3, "name": "Rahul"}
]
Nested JSON
JSON supports unlimited nesting. Objects can contain other objects, arrays can contain objects, and objects can contain arrays. Here is a more complex real-world example โ a product in an e-commerce API:
{
"productId": "SKU-9823",
"name": "Wireless Mechanical Keyboard",
"price": 4999.00,
"currency": "INR",
"inStock": true,
"category": {
"id": 5,
"name": "Computer Peripherals",
"slug": "computer-peripherals"
},
"tags": ["wireless", "mechanical", "gaming", "rgb"],
"variants": [
{"color": "Black", "stock": 45},
{"color": "White", "stock": 12},
{"color": "Silver", "stock": 0}
],
"rating": {
"average": 4.3,
"count": 289
}
}
Common JSON Mistakes
These are the most frequent errors when writing JSON by hand:
- Trailing commas โ
{"name": "Arjun",}โ the comma after the last item is invalid - Single quotes โ
{'name': 'Arjun'}โ JSON requires double quotes only - Unquoted keys โ
{name: "Arjun"}โ keys must always be in double quotes - Comments โ JSON does not support
// commentsor/* block comments */ - Undefined values โ JavaScript's
undefinedis not a valid JSON value โ usenullinstead
JSON vs XML
Before JSON became dominant, XML was the standard for data exchange. JSON won because it is much more compact and readable. Here is the same data in both formats:
// XML (verbose)
<user>
<id>1</id>
<name>Arjun</name>
<city>Bengaluru</city>
</user>
// JSON (compact)
{"id": 1, "name": "Arjun", "city": "Bengaluru"}
JSON is roughly 30-40% more compact than equivalent XML, faster to parse, and directly maps to native data structures in most programming languages.
Working with JSON in JavaScript
JavaScript provides two built-in methods for working with JSON:
// Parse a JSON string into a JavaScript object
const jsonString = '{"name": "Arjun", "age": 28}';
const obj = JSON.parse(jsonString);
console.log(obj.name); // "Arjun"
// Convert a JavaScript object to a JSON string
const user = { name: "Priya", age: 25 };
const json = JSON.stringify(user);
console.log(json); // '{"name":"Priya","age":25}'
// Pretty-print with indentation
const pretty = JSON.stringify(user, null, 2);
console.log(pretty);
// {
// "name": "Priya",
// "age": 25
// }
Working with JSON in Python
import json
# Parse JSON string
json_str = '{"name": "Arjun", "age": 28}'
obj = json.loads(json_str)
print(obj["name"]) # Arjun
# Convert Python dict to JSON string
user = {"name": "Priya", "age": 25}
json_output = json.dumps(user, indent=2)
print(json_output)
JSON in REST APIs
JSON is the standard format for REST API requests and responses. When you call an API like:
POST https://api.example.com/users
Content-Type: application/json
{
"name": "Arjun Sharma",
"email": "arjun@example.com",
"role": "developer"
}
The server responds with JSON:
{
"success": true,
"data": {
"id": 1042,
"name": "Arjun Sharma",
"email": "arjun@example.com",
"createdAt": "2025-01-15T10:30:00Z"
}
}
Summary
JSON is a simple, powerful, universally supported data format that every developer needs to understand. It has six data types, strict syntax rules, and excellent language support. Use our free JSON Formatter tool to validate and format your JSON instantly in your browser โ no signup required.