JSON (JavaScript Object Notation) is everywhere. Every API you call returns it. Every config file uses it. Every database stores it. If you work with software in any capacity, you will encounter JSON daily.
The good news: JSON is one of the simplest data formats to learn. If you can read a grocery list, you can read JSON. This guide takes you from zero to confidently reading, writing, and working with JSON.
What is JSON?
JSON is a text format for storing and transmitting structured data. It is human-readable and machine-parseable. It looks like this:
{
"name": "SamNet",
"role": "Network Engineer",
"website": "samnet.dev",
"skills": ["networking", "security", "automation"],
"experience_years": 5,
"remote": true
}
That is a JSON object. It describes a person using key-value pairs. Every programming language can read and write JSON, which is why it became the universal data exchange format.
JSON stands for JavaScript Object Notation, but do not let the name fool you — it is language-independent. Python, Go, Rust, Java, C#, PHP, Ruby, and every other language supports JSON natively.
JSON Syntax Rules
JSON has exactly six data types and a handful of rules:
Data Types
| Type | Example | Description |
|---|---|---|
| String | "hello" |
Text in double quotes |
| Number | 42, 3.14, -7 |
Integer or decimal (no quotes) |
| Boolean | true, false |
Lowercase, no quotes |
| Null | null |
Empty/missing value, no quotes |
| Object | {"key": "value"} |
Curly braces, key-value pairs |
| Array | [1, 2, 3] |
Square brackets, ordered list |
The Rules
- Keys must be strings in double quotes:
"name"notnameor'name' - Strings use double quotes only:
"hello"not'hello' - No trailing commas:
{"a": 1, "b": 2}not{"a": 1, "b": 2,} - No comments: JSON does not support
//or/ /comments - No undefined: Use
nullfor missing values - Numbers cannot have leading zeros:
42not042
Objects
An object is a collection of key-value pairs wrapped in curly braces {}:
{
"hostname": "web-server-01",
"ip": "10.0.0.35",
"port": 443,
"ssl": true,
"os": "Ubuntu 22.04"
}
Keys are always strings. Values can be any JSON type — including other objects and arrays.
Arrays
An array is an ordered list of values wrapped in square brackets []:
{
"dns_servers": ["1.1.1.1", "8.8.8.8", "9.9.9.9"],
"open_ports": [22, 80, 443, 8080],
"features": [
{"name": "SSL", "enabled": true},
{"name": "HTTP2", "enabled": true},
{"name": "IPv6", "enabled": false}
]
}
Arrays can contain any type: strings, numbers, objects, other arrays, or a mix.
Nested Structures
JSON's power comes from nesting. Objects contain objects. Arrays contain objects. Objects contain arrays of objects. Here is a realistic example — a server inventory:
{
"datacenter": "DFW-01",
"servers": [
{
"hostname": "web-01",
"role": "web",
"ip": "10.0.0.10",
"specs": {
"cpu_cores": 4,
"ram_gb": 16,
"disk_gb": 500
},
"services": ["nginx", "php-fpm", "redis"]
},
{
"hostname": "db-01",
"role": "database",
"ip": "10.0.0.20",
"specs": {
"cpu_cores": 8,
"ram_gb": 64,
"disk_gb": 2000
},
"services": ["postgresql", "pgbouncer"]
}
],
"last_updated": "2026-04-02T10:30:00Z"
}
To access the first server's RAM: data.servers[0].specs.ram_gb → 16
Working with JSON in Code
JavaScript
// Parse JSON string to object
const data = JSON.parse('{"name": "Sam", "age": 30}');
console.log(data.name); // "Sam"
// Convert object to JSON string
const json = JSON.stringify({name: "Sam", age: 30}, null, 2);
console.log(json);
// Fetch JSON from API
const response = await fetch("https://api.example.com/data");
const result = await response.json();
Python
import json
# Parse JSON string
data = json.loads('{"name": "Sam", "age": 30}')
print(data["name"]) # "Sam"
# Convert dict to JSON string
text = json.dumps({"name": "Sam", "age": 30}, indent=2)
# Read JSON file
with open("config.json") as f:
config = json.load(f)
# Write JSON file
with open("output.json", "w") as f:
json.dump(data, f, indent=2)
Command Line (jq)
jq is a powerful command-line JSON processor:
# Pretty-print JSON
curl -s https://api.example.com/data | jq .
# Extract a field
echo '{"name": "Sam"}' | jq '.name'
# Filter an array
echo '[{"port": 80}, {"port": 443}]' | jq '.[].port'
JSON in APIs
Almost every modern API uses JSON for requests and responses. Here is a typical pattern:
Request:
POST /api/users
Content-Type: application/json
{
"name": "Sam",
"email": "[email protected]",
"role": "admin"
}
Response:
{
"id": 42,
"name": "Sam",
"email": "[email protected]",
"role": "admin",
"created_at": "2026-04-02T10:30:00Z"
}
The Content-Type: application/json header tells the server to expect JSON in the request body.
Common Mistakes
Trailing Comma
// WRONG
{"name": "Sam", "age": 30,}
// CORRECT
{"name": "Sam", "age": 30}
Single Quotes
// WRONG
{'name': 'Sam'}
// CORRECT
{"name": "Sam"}
Unquoted Keys
// WRONG
{name: "Sam"}
// CORRECT
{"name": "Sam"}
Comments
// WRONG — JSON does not support comments
{
"port": 443 // HTTPS port
}
// CORRECT — no comments allowed
{
"port": 443
}
If you need comments in a config file, use JSONC (JSON with Comments, supported by VS Code), YAML, or TOML instead.
Numbers as Strings
// This is a string, not a number:
{"port": "443"}
// This is a number:
{"port": 443}
Be careful — "443" and 443 are different types. APIs may reject the wrong type.
JSON vs Other Formats
| Format | Human Readable | Comments | Use Case |
|---|---|---|---|
| JSON | Yes | No | APIs, data exchange |
| YAML | Very | Yes | Config files, Docker Compose |
| TOML | Yes | Yes | Config files (Rust, Python) |
| XML | Verbose | Yes | Legacy APIs, SOAP |
| CSV | Tabular only | No | Spreadsheet data |
Format and Validate Your JSON
Use our free JSON Formatter to prettify, minify, and validate JSON data. Paste messy JSON and get clean, indented output instantly. For converting between formats, try our CSV to JSON converter.