JSON: The Complete Developer Guide
JSON (JavaScript Object Notation) is the most widely used data format on the web. Every REST API you call, every configuration file you edit, and most database responses you handle use JSON. Understanding JSON deeply is a fundamental skill for every developer.
What Is JSON?
JSON is a lightweight, text-based data interchange format. Despite its name containing “JavaScript,” JSON is language-independent and supported by virtually every programming language. It was derived from JavaScript object literal syntax by Douglas Crockford in the early 2000s and standardized as ECMA-404 and RFC 8259.
A JSON value must be one of six types:
- String —
"hello world"(always double-quoted) - Number —
42,3.14,-1,1.5e10 - Boolean —
trueorfalse - Null —
null - Object —
{"key": "value"}(unordered key-value pairs) - Array —
[1, 2, 3](ordered list of values)
JSON Syntax Rules
JSON has strict syntax rules that differ from JavaScript object literals:
- Keys must be double-quoted strings —
'name'andnameare invalid; only"name"works. - No trailing commas —
{"a": 1, "b": 2,}throws a parse error. - No comments — JSON does not support
//or/* */comments. - No undefined —
undefinedis not a valid JSON value; usenullinstead. - No single quotes — Only double quotes are valid for strings and keys.
These restrictions make JSON unambiguous and easy to parse, which is why it became the standard for data exchange.
Parsing and Serialization
Every language provides built-in JSON support:
// JavaScript
const obj = JSON.parse('{"name": "Alice", "age": 30}');
const str = JSON.stringify(obj, null, 2); // Pretty-print with 2-space indent
# Python
import json
data = json.loads('{"name": "Alice", "age": 30}')
text = json.dumps(data, indent=2)
The second argument to JSON.stringify is a replacer function or array that controls which properties are included. The third argument controls indentation — use 2 for readable output or omit it for minified output.
Try it yourself: Paste any JSON into our JSON Formatter & Validator to instantly beautify, validate, or minify it — no signup required.
Common JSON Mistakes
Trailing Commas
{
"name": "Alice",
"age": 30,
}
This is valid JavaScript but invalid JSON. Remove the comma after the last property.
Single Quotes
{'name': 'Alice'}
JSON requires double quotes. This is a JavaScript object literal, not JSON.
Unquoted Keys
{name: "Alice"}
Every key in JSON must be a double-quoted string.
JSON vs Other Formats
JSON is not the only data format. Depending on your use case, other formats may be more appropriate:
- JSON vs XML — XML supports attributes, namespaces, and comments but is verbose. JSON is simpler and dominant in REST APIs.
- JSON vs YAML — YAML supports comments and is more readable for configuration files. Convert between them with our YAML to JSON Converter.
- JSON vs CSV — CSV is better for tabular data and spreadsheet workflows. Convert with our JSON to CSV Converter.
Validating JSON with JSON Schema
For production applications, you should validate JSON data against a schema. JSON Schema is a vocabulary that lets you define the expected structure, types, and constraints of your JSON data.
Best Practices
- Use consistent naming — Choose
camelCaseorsnake_caseand stick with it across your API. Our String Case Converter helps convert between conventions. - Keep responses flat — Deeply nested objects are hard to work with. Normalize when possible.
- Use
nullfor missing values — Don’t omit keys; explicitly set them tonull. - Validate on both sides — Validate JSON on both client and server to prevent data corruption.
- Pretty-print for debugging — Use
JSON.stringify(data, null, 2)during development, or paste into our JSON Formatter.