What is JSON
JSON (JavaScript Object Notation) is a lightweight data interchange format. Front-end/back-end communication, configuration files, and API responses all use JSON. With its clear structure, human readability, and machine parseability, it is the de facto standard of modern development.
JSON Syntax Rules
Data Types
- String: wrapped in double quotes, e.g. "hello"
- Number: integer or float, e.g. 42, 3.14
- Boolean: true or false
- Null: null
- Array: wrapped in square brackets, e.g. [1, 2, 3]
- Object: wrapped in curly braces, key-value pairs, e.g. {"name": "Tom"}
- Nesting: objects and arrays can be nested within each other
Basic Rules
- Keys must use double quotes (not single quotes)
- Strings must use double quotes
- No trailing commas (no comma after the last element)
- No comments allowed
- The top level can only be an object or array
Three Formatting Needs
Validation
Check whether the JSON syntax is correct. A missing comma, an extra comma, or mismatched quotes will all cause parsing failures.
Beautify (Pretty Print)
Expand a single-line compressed JSON into a multi-line format with indentation, making it human-readable.
Compressed form:
{"name":"Tom","age":20,"tags":["a","b"]}
Beautified:
{
"name": "Tom",
"age": 20,
"tags": ["a", "b"]
}
Minify
Remove all spaces and line breaks from the beautified format, compressing it into a single line to reduce size. Used for transmission and storage.
Common JSON Errors
Quote Errors
- Using single quotes: JSON only allows double quotes
- Forgetting to close quotes
- Not quoting key names
Wrong: {'name': 'Tom'} (single quotes) Correct: {"name": "Tom"}
Comma Errors
- Trailing comma: an extra comma after the last element
- Missing comma: forgetting a comma between elements
Wrong: {"a": 1, "b": 2,} (trailing comma) Correct: {"a": 1, "b": 2}
Data Type Errors
- Using undefined (JSON has no undefined; use null)
- Using NaN / Infinity (JSON doesn't support them)
- Functions and Date objects can't be serialized directly
Escape Errors
Special characters inside strings must be escaped:
- Quote: "
- Backslash: \
- Newline: \n
- Tab: \t
- Carriage return: \r
Wrong: "He said "hello"" (quotes not escaped) Correct: "He said "hello""
Encoding Errors
JSON must use UTF-8 encoding. Other encodings may cause garbled text or parsing failures.
Nesting and Depth
Nesting Example
{
"user": {
"name": "Tom",
"address": {
"city": "Beijing",
"zip": "100000"
}
},
"orders": [
{"id": 1, "items": ["A", "B"]},
{"id": 2, "items": ["C"]}
]
}
Indentation
- 2-space indentation: most common
- 4-space indentation: more spacious
- Tab indentation: some teams use it
Keep indentation style consistent when beautifying.
Deep Nesting
Nesting beyond 5 levels becomes hard to read. Consider flattening the structure or splitting it up.
JSON vs Other Formats
JSON vs JSONL
- JSON: the entire file is one JSON (usually an object or array)
- JSONL (JSON Lines): each line is an independent JSON, suitable for logs and streaming data
JSON vs JSON5
JSON5 is a superset of JSON that supports:
- Single quotes
- Trailing commas
- Comments
- Hexadecimal numbers
But JSON5 is not standard JSON, and parser compatibility is limited.
JSON vs YAML
- JSON: strict syntax, machine-friendly
- YAML: lenient syntax (supports comments, indentation for hierarchy), human-friendly
- Config files often use YAML; API transmission uses JSON
JSON vs XML
- JSON: lightweight, no tag overhead
- XML: verbose tags, but supports attributes and namespaces
- In modern development, JSON has largely replaced XML
Practical Tips
Copying JSON
Copy JSON from API responses or logs, paste it into a formatting tool to beautify, and read it easily.
Debugging JSON
When an API returns an error, format the response JSON and check whether the fields are correct.
Configuration Files
Write config files in JSON and annotate the meaning of each field after formatting (note that standard JSON doesn't support comments—use JSONC or separate documentation).
Data Conversion
In scenarios like CSV to JSON or XML to JSON, format and validate the result after conversion.
Use the CSV to JSON tool to convert tabular data to JSON in one click.
Using Formatting Tools
Validation
Paste JSON, and the tool checks the syntax. If there's an error, it points out the location and cause.
Beautify
Paste compressed JSON, choose indentation, click beautify, and get multi-line output.
Minify
Paste beautified JSON, click minify, and get single-line output.
Escaping
Escape special characters in JSON strings, or reverse the process.
Handling JSON with DocsAll
JSON involves encoding and conversion. Use the relevant tools:
- Encode/Decode tool: handle JSON string escaping
- CSV to JSON tool: convert tables to JSON
Processed in the browser—data is not uploaded.
FAQ
Chinese characters become \uXXXX after formatting
Some tools escape Chinese characters by default. To display Chinese text, turn off the Unicode escape option.
Large JSON lags
Formatting very large JSON (several MB) may lag. Use a command-line tool (like jq) for better efficiency.
What about comments?
Standard JSON doesn't support comments. If you need comments, use JSONC, JSON5, or YAML.
Summary
JSON formatting comes down to three things: validate syntax, beautify for readability, minify for transmission. Common errors involve quotes, commas, and escaping. Handle JSON with the DocsAll Encode/Decode tool and CSV to JSON tool—running in the browser, with no data leaving your device.