JSON for Developers: Syntax, Validation, and Common Mistakes
· 10 min read
JSON Syntax Rules
JSON (JavaScript Object Notation) is the most widely used data interchange format. It is language-independent, human-readable, and supported by virtually every programming language.
{
"name": "RunDev",
"version": 2,
"features": ["formatter", "validator", "converter"],
"config": {
"theme": "dark",
"autoSave": true
},
"deprecated": null
}
Strict rules that trip up developers:
- Keys MUST be double-quoted strings —
"name"notnameor'name' - Strings MUST use double quotes —
"hello"not'hello' - No trailing commas —
[1, 2, 3]not[1, 2, 3,] - No comments — not
// commentor/* comment */ - No undefined — use
nullinstead - Numbers cannot have leading zeros —
0.5not.5,10not010
Data Types
| Type | Example | Notes |
|---|---|---|
| String | "hello world" | Must be double-quoted. Supports \n, \t, \u0041 escapes. |
| Number | 42, 3.14, -1, 1e10 | No hex, octal, or Infinity/NaN. Integer and float are the same type. |
| Boolean | true, false | Lowercase only. Not "true" (that's a string). |
| Null | null | Lowercase only. Represents absence of value. |
| Array | [1, "two", true] | Ordered list. Can mix types (but usually shouldn't). |
| Object | {"key": "value"} | Unordered key-value pairs. Keys must be unique (per spec). |
Common Mistakes
| Mistake | Wrong | Correct |
|---|---|---|
| Single quotes | {'name': 'test'} | {"name": "test"} |
| Trailing comma | {"a": 1, "b": 2,} | {"a": 1, "b": 2} |
| Unquoted keys | {name: "test"} | {"name": "test"} |
| Comments | {"a": 1 // comment} | Remove comments or use JSONC |
| Undefined | {"a": undefined} | {"a": null} |
| Leading zeros | {"port": 080} | {"port": 80} |
| Multiline strings | "line1\nline2" (literal newline) | "line1\\nline2" (escaped) |
Paste your JSON into our JSON Formatter to instantly find and fix syntax errors.
Validation
Command Line
# Python (built-in)
python3 -m json.tool data.json
# jq (powerful JSON processor)
jq . data.json
# Node.js
node -e "console.log(JSON.parse(require('fs').readFileSync('data.json','utf8')))"
JSON Schema
JSON Schema defines the structure, types, and constraints of your JSON data:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"required": ["name", "version"],
"properties": {
"name": { "type": "string", "minLength": 1 },
"version": { "type": "integer", "minimum": 1 },
"tags": {
"type": "array",
"items": { "type": "string" }
}
}
}
JSON Tools for Developers
| Task | Tool | Command |
|---|---|---|
| Format/prettify | jq, python | jq . file.json |
| Minify | jq | jq -c . file.json |
| Query/filter | jq | jq '.users[] | .name' file.json |
| Convert to CSV | jq + miller | jq -r '.[] | [.a,.b] | @csv' file.json |
| Diff two files | jd, diff | diff <(jq -S . a.json) <(jq -S . b.json) |
| Validate schema | ajv | ajv validate -s schema.json -d data.json |
Or use our browser-based tools: JSON Formatter, JSON Diff, JSON to CSV, JSONPath Tester.
JSON Alternatives
| Format | Advantage over JSON | Use Case |
|---|---|---|
| YAML | Comments, multiline strings, anchors | Config files (Docker, K8s, CI/CD) |
| TOML | Comments, cleaner syntax for config | Rust (Cargo.toml), Python (pyproject.toml) |
| XML | Schemas, namespaces, mixed content | SOAP APIs, document formats |
| Protocol Buffers | Binary, typed, much smaller | gRPC, high-performance APIs |
| MessagePack | Binary JSON, smaller and faster | Real-time applications |
| JSON5 | Comments, trailing commas, unquoted keys | Config files that need JSON compatibility |
Frequently Asked Questions
What is the difference between JSON and JavaScript objects?
JSON keys must be double-quoted strings. JavaScript objects allow unquoted keys, single quotes, trailing commas, computed properties, and methods. JSON is a data interchange format; JavaScript objects are language constructs.
Can JSON have comments?
No. Standard JSON (RFC 8259) does not support comments. Use JSONC (JSON with Comments, supported by VS Code), JSON5, or YAML if you need comments. A common workaround is using a "_comment" key.
What is the maximum size of a JSON file?
JSON has no inherent size limit. Practical limits depend on the parser: browsers handle ~500MB, Node.js default string limit is ~1.7GB. For large datasets, consider JSON Lines (JSONL) format or streaming parsers like SAX-style JSON parsers.
Why does JSON not support dates?
JSON only defines six data types: string, number, boolean, null, array, and object. Dates are typically represented as ISO 8601 strings ("2026-03-29T12:00:00Z") or Unix timestamps (1774972800).
Is JSON better than XML?
For web APIs and data exchange: JSON is lighter, easier to parse, and more readable. XML is better when you need schemas (XSD), namespaces, attributes, or mixed content. Most modern APIs use JSON.