JSON (JavaScript Object Notation) is a lightweight text format for storing and transporting data. It was created by Douglas Crockford in the early 2000s and has since become the most widely used data interchange format on the web — replacing XML in most modern APIs.
Despite its name, JSON is completely language-independent. Every major programming language can parse and generate JSON: Python, Java, Go, Rust, PHP, Ruby, C#, and of course JavaScript.
JSON Syntax Rules
JSON has only 6 data types and a very strict syntax. Understanding these rules prevents the most common bugs:
- Strings — must use double quotes only. Single quotes are invalid.
- Numbers — integers or decimals. No NaN or Infinity.
- Booleans — lowercase
trueorfalse. NotTrueorTrue. - null — lowercase only. Not
NullorNULL. - Arrays — ordered list in square brackets
[...]. - Objects — unordered key-value pairs in curly braces
{...}. Keys must be strings.
{
"name": "Alice",
"age": 30,
"active": true,
"score": 98.5,
"address": null,
"tags": ["developer", "designer"],
"location": {
"city": "Mumbai",
"country": "India"
}
}Common JSON Mistakes
| Mistake | Wrong | Correct |
|---|---|---|
| Single quotes | {'name': 'Alice'} | {"name": "Alice"} |
| Trailing comma | {"a":1, "b":2,} | {"a":1, "b":2} |
| Comments | // comment {"a":1} | {"a":1} // not allowed |
| Undefined value | {"x": undefined} | {"x": null} |
| Uppercase boolean | {"active": True} | {"active": true} |
JSON vs Other Formats
| Feature | JSON | XML | YAML |
|---|---|---|---|
| Human-readable | ✅ Yes | ⚠️ Verbose | ✅ Best |
| Comments | ❌ None | ✅ Yes | ✅ Yes |
| Type system | ✅ 6 types | ❌ Strings only | ✅ Rich |
| Browser native | ✅ Yes | ❌ No | ❌ No |
| API standard | ✅ Dominant | ⚠️ Legacy | ❌ Rare |
Why JSON Won the Web
JSON won because of three things: it is natively supported in JavaScript (the language of the web), it is much smaller and faster than XML, and its syntax maps directly to data structures developers already use (dictionaries, lists, strings).
REST APIs use JSON. GraphQL uses JSON. LocalStorage uses JSON. Config files use JSON. Understanding JSON is non-negotiable for any modern developer.
Parsing JSON in Different Languages
// JavaScript
const obj = JSON.parse('{"name":"Alice"}');
const str = JSON.stringify(obj, null, 2);# Python
import json
obj = json.loads('{"name":"Alice"}')
s = json.dumps(obj, indent=2)