help / json
JSON Formatting: Complete Guide
JSON (JavaScript Object Notation) is a lightweight, text-based data format used for data exchange between applications and APIs. It is easy for humans to read and write, and easy for machines to parse. However, JSON returned by APIs is often minified — stripped of all whitespace to reduce response size — making it difficult to read and debug.
JSON syntax rules
JSON has strict syntax requirements. Violating any of these rules produces invalid JSON that will fail to parse:
- All keys must be double-quoted strings.
{"key": "value"}is valid;{key: "value"}and{'key': 'value'}are not. - No trailing commas. The last element in an array or object must not be followed by a comma.
[1, 2, 3,]is invalid. - No comments. JSON has no comment syntax.
// commentcauses a parse error. - Strings must use double quotes. Single-quoted strings are not valid JSON.
- Numbers must not have leading zeros.
007is invalid; use7. - Valid value types: string, number, boolean (
true/false), null, object, array.undefined, functions, and Date objects are not valid JSON values.
Pretty-printing JSON
To format JSON in code:
# Python import json print(json.dumps(data, indent=2)) # JavaScript console.log(JSON.stringify(data, null, 2)); # Command line (jq) cat data.json | jq . # Command line (Python one-liner) python3 -m json.tool data.json
The second argument to JSON.stringify is a replacer (pass null to include all fields), and the third is the indentation level in spaces. indent=2 is the most common convention.
Minifying JSON
Minification removes all unnecessary whitespace. In JavaScript: JSON.stringify(JSON.parse(json)) — parse then re-stringify with no indent argument. In Python: json.dumps(data, separators=(',', ':')). At the command line: jq -c . data.json (compact output).
Validating JSON
Validation confirms that a string is syntactically valid JSON. Every language standard library includes a JSON parser that raises an error on invalid input:
- JavaScript:
JSON.parse(str)throwsSyntaxErroron invalid JSON. - Python:
json.loads(str)raisesjson.JSONDecodeError. - Go:
json.Unmarshal([]byte(str), &v)returns an error.
The error message usually includes the line and column where parsing failed, which helps locate the invalid character or missing quote.
Common debugging tips
If you're getting a parse error and can't find it: paste the JSON into a formatter tool — it will highlight the exact location of the error. The most common causes are: a trailing comma in an array or object, a property key with single quotes instead of double quotes, or a missing closing brace or bracket.
When working with JSON from external sources (APIs, database exports), always validate before processing. An unexpected null or missing field is much easier to debug when caught at the parsing stage.
Try it now → Use the JSON Formatter to beautify, minify, or validate any JSON instantly in your browser. Your data never leaves your device.