What is JSON?
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write, and easy for machines to parse and generate. JSON is widely used in APIs, configuration files, and data storage because it's language-independent but uses conventions familiar to programmers of the C-family of languages (C, C++, JavaScript, Python, etc.).
Why Format JSON?
Raw JSON responses from APIs or data exports are often minified (compressed onto a single line without spaces) to save bandwidth. While efficient for computers, this makes it nearly impossible for humans to read or debug. A JSON formatter adds indentation (usually spaces or tabs) and line breaks, transforming an unreadable blob into a structured, hierarchical view.
- Readability: Quickly understand the nested structure of objects and arrays.
- Debugging: Easily spot missing brackets, commas, or incorrect data types.
- Validation: Ensure your JSON string conforms to the strict JSON standard.
JSON Syntax Rules
To be valid, JSON must adhere to strict rules. Common causes of "Invalid JSON" errors include:
| Rule | Example of Valid JSON | Common Error |
|---|---|---|
| Double Quotes Only | {"key": "value"} |
Using single quotes {'key': 'value'} |
| Keys Must Be Quoted | {"name": "Gab"} |
Missing quotes {name: "Gab"} |
| No Trailing Commas | [1, 2, 3] |
Extra comma at the end [1, 2, 3,] |
| Specific Data Types | "string", 123, true, null |
Using undefined or functions |