If you've spent more than a week writing code that talks to an API or reads a config file, you've almost certainly run into JSON. It's everywhere — REST APIs, configuration files, databases like MongoDB, browser localStorage, and half the tooling you use every day. Yet many developers use it without fully understanding what it is or why it works the way it does. This guide fixes that.
What Is JSON?
JSON stands for JavaScript Object Notation. Despite the name, it has nothing to do with JavaScript at runtime — it's a plain text format for representing structured data. It was created by Douglas Crockford in the early 2000s as a lightweight alternative to XML, and it took off fast because it's both human-readable and trivially easy for machines to parse.
The official specification lives at json.org and ECMA-404. Every modern programming language has a built-in JSON parser: JSON.parse() in JavaScript, json.loads() in Python, json_decode() in PHP, and so on.
JSON Syntax Rules
JSON has exactly six value types and a handful of rules. If you memorize these, you'll never be confused by a JSON file again:
- Data is in name/value pairs
- Data is separated by commas
- Curly braces hold objects
- Square brackets hold arrays
- Strings must use double quotes (not single quotes)
- No trailing commas allowed
- No comments allowed (unlike JavaScript)
The Six JSON Data Types
1. String
A sequence of Unicode characters enclosed in double quotes.
"name": "Alice"
"city": "Islamabad"
"emoji": "❤"
2. Number
JSON numbers can be integers or floating-point. No distinction between int and float at the type level.
"age": 30
"price": 9.99
"temperature": -3.5
"scientific": 1.5e10
3. Boolean
Either true or false — lowercase, no quotes.
"isActive": true
"isDeleted": false
4. Null
Represents the absence of a value. Written as null — lowercase, no quotes.
"middleName": null
5. Array
An ordered list of values. Values can be any type, including mixed types (though mixing types in an array is usually a sign of a poor data model).
"tags": ["php", "javascript", "api"]
"scores": [98, 75, 84, 91]
"mixed": [1, "hello", true, null]
6. Object
An unordered collection of name/value pairs. This is the workhorse of JSON.
{
"user": {
"id": 42,
"name": "Alice",
"email": "alice@example.com",
"roles": ["admin", "editor"],
"address": {
"city": "Islamabad",
"country": "PK"
}
}
}
A Real-World JSON Example
Here's the kind of JSON response you'd get back from a typical REST API endpoint like GET /users/42:
{
"status": "success",
"data": {
"id": 42,
"username": "alice_dev",
"email": "alice@example.com",
"createdAt": "2024-01-15T09:30:00Z",
"isVerified": true,
"subscription": {
"plan": "pro",
"expiresAt": "2027-01-15T00:00:00Z",
"autoRenew": true
},
"tags": ["developer", "beta-tester"]
},
"meta": {
"requestId": "a3f8b2c1",
"processingTime": 12
}
}
Parsing JSON in Different Languages
One of JSON's biggest strengths is how easy it is to work with across languages. Here's a quick comparison:
// JavaScript
const data = JSON.parse('{"name":"Alice","age":30}');
console.log(data.name); // "Alice"
const json = JSON.stringify(data, null, 2); // pretty-print
// PHP
$data = json_decode('{"name":"Alice","age":30}', true);
echo $data['name']; // Alice
$json = json_encode($data, JSON_PRETTY_PRINT);
# Python
import json
data = json.loads('{"name":"Alice","age":30}')
print(data['name']) # Alice
output = json.dumps(data, indent=2)
JSON vs XML: When to Use Which
XML was the dominant data interchange format before JSON took over. Here's the honest comparison:
- Verbosity: JSON is significantly more compact. The same data in XML can be 2–3x larger.
- Readability: JSON wins for most developers. XML's angle bracket syntax adds visual noise.
- Schema validation: XML has mature schema tools (XSD, DTD). JSON has JSON Schema, which is good but less universal.
- Document markup: If you're mixing data with markup (like rich text), XML is a better fit. That's why HTML and SVG use XML-based syntax.
- Comments: XML supports comments. JSON does not — this is its biggest practical annoyance in config files.
In 2026, JSON is the default for web APIs, configuration, and data storage. Use XML when you're working with legacy enterprise systems, document formats (DOCX, SVG), or need rich schema validation tooling.
Common JSON Mistakes
- Using single quotes instead of double quotes
- Trailing commas after the last item in an array or object
- Adding comments (use JSON5 or JSONC if you need comments)
- Using undefined or Infinity values (these aren't valid JSON)
- Forgetting that object key order is not guaranteed
Need to format, validate, or prettify a JSON document right now?
Try the Free JSON Formatter
CREESOL