How to Format and Validate JSON (and Fix Common Errors)
JSON is a text format for data made of objects in curly braces, arrays in square brackets, and values that are strings, numbers, true, false or null. To format JSON, paste it into a formatter that parses it and prints it with indentation. If parsing fails, the data has a syntax error, and the error message tells you roughly where.
What valid JSON looks like
- Keys are strings in double quotes: "name".
- Strings use double quotes, never single quotes.
- Items are separated by commas, with no comma after the last item.
- Numbers have no quotes and no leading zeros.
- true, false and null are lowercase.
Formatting (pretty-printing)
Compact JSON from an API is hard to read. The JSON formatter indents it with two spaces per level:
{
"name": "InovoTools",
"active": true
}
Minifying
For sending or storing data, remove the whitespace. The JSON minifier turns the example above back into {"name":"InovoTools","active":true}, which is smaller to transfer.
The most common errors
| Error | Example | Fix |
|---|---|---|
| Trailing comma | {"age": 36,} | Remove the last comma |
| Single quotes | {'name': 'Ada'} | Use double quotes |
| Unquoted key | {name: "Ada"} | Quote the key |
| Comments | // note | JSON has no comments; remove them |
| Missing comma | {"a": 1 "b": 2} | Add a comma between items |
For {"name": "Ada", "age": 36,}, the JSON validator reports that it expected a double-quoted property name at the position of the trailing comma. The position helps you find the problem in a long file.
Converting JSON
An array of objects with the same keys converts neatly to a table. The JSON to CSV converter turns [{"name":"Ada","role":"Engineer"},{"name":"Lin","role":"Designer"}] into a header row of name,role and one row per object, ready for a spreadsheet. For configuration files, the JSON to YAML converter produces YAML.
Privacy
Formatting JSON in a browser tool that does not upload your data is safer for API keys and customer data. The tools here run in your browser.
Questions and answers
Why is my JSON invalid?
The most common causes are a trailing comma, single quotes, unquoted keys and comments. A validator shows the position of the first error.
Can JSON have comments?
No. Standard JSON does not allow comments. Some tools accept them, but strict parsers will reject the file.