What is JSON?
JSON (JavaScript Object Notation) is the most popular data interchange format for Web APIs, accounting for over 90% of API traffic. Based on ECMA-404 and RFC 8259 standards, its simple, readable key-value structure has become the de facto standard for frontend-backend communication.
JSON Syntax Rules
A valid JSON document must follow these strict rules:
- Keys must be double-quoted:
"key"notkeyor'key' - Strings must be double-quoted:
"value"not'value' - No trailing commas: The last element must not have a trailing comma
- No comments: JSON does not natively support comments
- UTF-8 encoding: This is the recommended standard encoding
JSON Data Types
- String: Double-quoted Unicode character sequence
- Number: Integer or floating-point, no quotes
- Boolean:
trueorfalse - Array: Ordered list in square brackets, e.g.,
[1, 2, 3] - Object: Key-value collection in curly braces, e.g.,
{"key": "value"} - null: Represents an empty value
JSON Formatting & Validation
Why Format?
In production, JSON is typically transmitted in minified form with all whitespace removed to reduce size. Minified JSON can reduce payload size by 30-50%, but becomes unreadable. Formatters convert compressed JSON into a well-structured hierarchical format, typically using 2-space indentation.
Validation Importance
JSON parsing errors are among the most common frontend bugs. Typical issues include:
- Trailing commas
- Missing quotes
- Mismatched brackets
- Single quotes instead of double quotes
- Invalid escape characters
JSON Schema Validation
JSON Schema is a powerful validation tool that defines structural constraints:
- Required fields specification
- Field types and formats
- Numeric ranges or string length limits
- Enumerated allowed values
- Nested object validation rules
Large JSON Processing
For JSON files over 100MB, avoid loading everything into memory. Use streaming parsers:
- simdjson: Up to 2.5 GB/s parsing speed
- ijson (Python): Lazy iteration parsing
JSON Security
- Never use
eval()to parse JSON - Use
JSON.parse()or standard library parsers - Watch for prototype pollution attacks (
__proto__) - Always validate untrusted JSON data