What is JSON — Simple Explanation with Examples
JSON (JavaScript Object Notation) is a lightweight, text-based data format for storing and exchanging structured data between systems, designed to be both human-readable and machine-parseable.
In this guide, you’ll learn how JSON works, how to read and write JSON in Python and JavaScript, and why it has become the universal data format for APIs. Whether you’re building a web app or integrating third-party services, JSON is unavoidable.
Why JSON Exists — The Problem It Solves
Before JSON, web applications exchanged data primarily using XML. XML is powerful but verbose — a simple key-value pair can take multiple lines. This made parsing slow and debugging painful. JSON was created in 2001 by Douglas Crockford as a simpler alternative that:
- Uses less bandwidth (smaller payload size)
- Is trivial to parse in JavaScript (eval before the spec)
- Maps naturally to common data structures (objects, arrays)
JSON vs XML
| Feature | JSON | XML |
|---|---|---|
| Readability | Clean | Verbose |
| Data types | Native (string, number, bool, null) | All text |
| Array support | Native [] | Requires wrapping elements |
| Parsing speed | Fast | Slower |
| Browser support | Native (JSON.parse) | Requires XML parser |
| Metadata/attributes | No | Yes (attributes, namespaces) |
// JSON — 102 bytes
{
"name": "Alice",
"age": 30,
"active": true,
"tags": ["admin", "editor"]
}<!-- XML — 195 bytes -->
<user>
<name>Alice</name>
<age type="integer">30</age>
<active type="boolean">true</active>
<tags>
<tag>admin</tag>
<tag>editor</tag>
</tags>
</user>The Analogy — Fill-in Form
JSON is like a fill-in form. Each field has a label (the key) and a space to write information (the value). A single form might have text fields, number fields, checkboxes (booleans), and sections that repeat (arrays).
If you imagine passing this form between two people — one fills it out, the other reads it — JSON is the standardized way to structure that form so both sides agree on how it looks.
JSON Structure
JSON supports exactly six data types:
| Type | Example | Description |
|---|---|---|
| String | "hello" | Double-quoted Unicode text |
| Number | 42 or 3.14 | Integer or floating-point |
| Boolean | true / false | Logical values |
| Null | null | Empty/absent value |
| Array | [1, 2, 3] | Ordered list (brackets) |
| Object | {"key": "value"} | Key-value map (braces) |
Rules
- Keys must be double-quoted strings (
"name", notnameor'name') - No trailing commas allowed
- No comments (JSON is data-only)
- Strings use double quotes only
- The outermost value must be an object, array, or literal
Example Document
{
"id": 42,
"title": "What is JSON",
"published": true,
"wordCount": 1500,
"tags": ["programming", "json", "tutorial"],
"author": {
"name": "DodaTech",
"email": "author@dodatech.com",
"active": true
},
"metadata": null
}JSON in APIs
Most modern REST APIs use JSON as their primary data format.
Request Example
POST /api/users HTTP/1.1
Content-Type: application/json
{
"email": "user@example.com",
"password": "s3cret!",
"name": "John Doe"
}Response Example
HTTP/1.1 201 Created
Content-Type: application/json
{
"id": 101,
"email": "user@example.com",
"name": "John Doe",
"createdAt": "2026-06-20T10:30:00Z"
}Parsing JSON in Python
import json
# Parse JSON string
json_str = '{"name": "Alice", "age": 30}'
data = json.loads(json_str) # Returns Python dict
print(data["name"]) # Alice
print(data["age"]) # 30
# Convert Python to JSON
person = {
"name": "Bob",
"age": 25,
"skills": ["Python", "JSON"]
}
json_output = json.dumps(person, indent=2)
print(json_output)Expected output:
{
"name": "Bob",
"age": 25,
"skills": [
"Python",
"JSON"
]
}Working with JSON Files
import json
# Read from file
with open("config.json") as f:
config = json.load(f)
# Write to file
config["version"] = "2.0"
with open("config.json", "w") as f:
json.dump(config, f, indent=2)Parsing JSON in JavaScript
// Parse JSON string
const jsonStr = '{"name": "Alice", "age": 30}';
const data = JSON.parse(jsonStr);
console.log(data.name); // Alice
console.log(data.age); // 30
// Convert object to JSON
const person = {
name: "Bob",
age: 25,
skills: ["JavaScript", "JSON"]
};
const jsonOutput = JSON.stringify(person, null, 2);
console.log(jsonOutput);Expected output: Same as Python example above.
JSON Validation Tools
Before using JSON in production, validate it:
- Online: JSONLint.com, JSON Schema Validator
- CLI:
python -m json.tool file.json - VS Code: Built-in JSON validation (red squiggles for errors)
# Validate a JSON file from command line
echo '{"valid": true}' | python -m json.tool
# Output: {
# "valid": true
# }
echo '{invalid}' | python -m json.tool
# Output: Expecting property name enclosed in double quotes: line 1 column 2Common Use Cases
1. REST API communication
JSON is the standard format for request/response bodies in modern web APIs. Services like GitHub, Stripe, and Twilio all use JSON.
2. Configuration files
npm’s package.json, VS Code’s settings.json, and Docker Compose files are all JSON (or JSON-compatible like JSONC with comments).
3. Data storage
NoSQL databases like MongoDB store documents in BSON (a binary variant of JSON). IndexedDB and localStorage in browsers use JSON for structured data.
4. Real-time data streaming
WebSocket messages are often JSON-encoded. Chat apps, live dashboards, and collaborative editors exchange JSON frames.
5. Machine-to-machine communication
IoT devices send sensor readings as JSON. Cloud functions parse JSON from event triggers. Webhooks deliver events as JSON payloads.
FAQ
Related Terms
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro