JSON (JavaScript Object Notation) is a lightweight and language-independent data-interchange format widely used in web development, APIs, and configuration files.
It allows systems to exchange structured data in a readable format, making it a universal data language across platforms.
This JSON Cheatsheet gives you a complete reference — from syntax basics to real-world examples — for developers working with JavaScript, Python, PHP, Node.js, and APIs.
Table of Contents:
What is JSON?
JSON is a text-based format for representing structured data. It uses simple syntax with key-value pairs, arrays, and nested objects.
✅ Key Benefits:
- Easy to read and write
- Lightweight and language-independent
- Compatible with most modern APIs
- Easily parsed into native data structures
Example of JSON:
{
"name": "John Doe",
"age": 30,
"skills": ["HTML", "CSS", "JavaScript"],
"active": true
}
JSON Syntax Rules
| Element | Description | Example |
|---|---|---|
| Data | Name/value pairs | "name": "Alice" |
| Objects | {} contain key/value pairs | {"city": "New York"} |
| Arrays | [] contain ordered values | ["red", "green", "blue"] |
| Keys | Must be strings, enclosed in double quotes | "language" |
| Values | Can be string, number, boolean, array, object, or null | "PHP", 123, true |
| Whitespace | Ignored by parser | Optional |
| Comma | Used to separate key/value pairs | "a": 1, "b": 2 |
json cheatsheet, json tutorial, json syntax, json examples, json parser, json objects and arrays, json format rules, json vs xml, json in javascript, json in php, json api response
JSON Data Types
| Type | Example |
|---|---|
| String | "language": "Python" |
| Number | "year": 2025 |
| Boolean | "active": true |
| Array | "colors": ["blue", "green", "red"] |
| Object | "user": {"id": 1, "name": "John"} |
| Null | "address": null |
JSON Objects and Arrays
Object Example:
{
"id": 101,
"title": "Developer",
"salary": 75000
}
Array Example:
[
"JavaScript",
"Python",
"PHP"
]
Array of Objects:
[
{ "id": 1, "name": "Alice" },
{ "id": 2, "name": "Bob" }
]
Nested JSON Example
{
"student": {
"name": "Sara",
"grade": "A",
"subjects": ["Math", "Science", "English"],
"address": {
"city": "New Delhi",
"country": "India"
}
}
}
Working with JSON in JavaScript
Parsing JSON
Convert JSON string → JavaScript object:
const jsonData = '{"name": "John", "age": 25}';
const obj = JSON.parse(jsonData);
console.log(obj.name); // John
Stringify JSON
Convert JavaScript object → JSON string:
const user = { name: "Alice", age: 22 };
const jsonString = JSON.stringify(user);
console.log(jsonString);
Working with JSON in PHP
<?php
$json = '{"name":"Alice","age":22}';
$data = json_decode($json, true); // Convert to PHP array
echo $data['name']; // Alice
$user = array("name"=>"John", "age"=>30);
echo json_encode($user);
?>
Working with JSON in Python
import json
data = '{"name": "John", "age": 30}'
parsed = json.loads(data)
print(parsed['name'])
person = {"name": "Alice", "city": "London"}
json_string = json.dumps(person)
print(json_string)
JSON in REST APIs
A typical API response in JSON:
{
"status": "success",
"data": {
"id": 101,
"title": "Learn JSON",
"category": "Programming"
}
}
JSON Formatting Tips
✅ Always use double quotes for keys and strings
✅ Avoid trailing commas
✅ Use online tools like phponline.in/json-formatter for clean formatting
✅ Validate JSON before using it in production
JSON vs XML
| Feature | JSON | XML |
|---|---|---|
| Readability | Easier | Verbose |
| Syntax | Key-value | Tags |
| Data Size | Smaller | Larger |
| Parsing Speed | Faster | Slower |
| Browser Support | Native in JS | Requires parser |
| Data Types | Supports multiple | All as string |
JSON Errors & Validation
Common issues:
- Missing double quotes
- Extra commas
- Unescaped characters
You can validate JSON using:
npm install -g jsonlint
jsonlint file.json
JSON Pretty Print
Format JSON for readability:
JSON.stringify(data, null, 2);
Best Practices for JSON
✅ Use lowercase keys for consistency
✅ Avoid deeply nested objects (affects performance)
✅ Keep structure consistent across APIs
✅ Include metadata like status, message, or timestamp
FAQ — JSON Cheatsheet
Q1: What is JSON used for?
JSON is used for data exchange between a client and server in web applications and APIs.
Q2: How is JSON different from JavaScript objects?
JSON is a text format, while JavaScript objects are in-memory structures.
Q3: Can JSON contain comments?
No, JSON doesn’t support comments — use metadata instead.
Q4: What tools can validate JSON?
You can use JSONLint, Postman, or online validators like json-validator.
Q5: How do I convert JSON to CSV?
You can use Python’s pandas library or online converters to transform JSON data into CSV.

