Development
JSON Formatting Guide: How to Format, Validate, and Debug JSON
Everything developers need to know about JSON formatting: syntax rules, indentation standards, common errors and how to fix them, when to minify vs beautify, and the fastest tools for the job.
What Is JSON and Why Formatting Matters
JSON (JavaScript Object Notation) is the standard data format for web APIs, configuration files, and data exchange between systems. It uses key-value pairs, nested objects, and arrays to represent structured data in a format that is both human-readable and machine-parseable. JSON is language-agnostic, which means every major programming language has built-in libraries to read and write it.
Raw JSON from an API response or a log file is almost always compact: a single line with no whitespace. This is efficient for network transmission and storage, but it is nearly impossible to read and debug when something goes wrong. A JSON formatter takes that compact string and adds indentation and line breaks to expose the structure so you can navigate it visually.
Formatting also catches syntax errors. The most common JSON bugs, such as a trailing comma, a missing quote, or a mismatched bracket, are invisible in a wall of compact text but immediately obvious in a formatted, indented view. Running every unfamiliar JSON payload through a formatter before working with it saves significant debugging time.
JSON Syntax Rules Every Developer Needs to Know
JSON has a strict syntax defined by RFC 8259. The rules that most developers violate are: keys must always be wrapped in double quotes (not single quotes), strings must use double quotes (not backticks or single quotes), and the last item in an object or array must not be followed by a comma (no trailing commas allowed).
Boolean values must be lowercase: true and false, not True or False or TRUE. Null must be lowercase: null, not NULL, None, or undefined. Numbers do not need quotes unless you want them treated as strings. JSON does not support comments. You cannot add single-line or block-style comments to a JSON file, and standard JSON parsers will reject them as invalid syntax.
These rules exist because JSON was designed for machine parsing above all else. Every valid JSON document can be parsed by every compliant parser in every language without ambiguity. The strictness is what makes it a reliable interchange format.
- Keys must be double-quoted: {"name": "value"} not {name: "value"}
- No trailing commas: [1, 2, 3] not [1, 2, 3,]
- Booleans and null are lowercase: true, false, null
- No comments are allowed in standard JSON
- Strings must use double quotes, not single quotes or backticks
Common JSON Syntax Errors and How to Fix Them
Trailing comma is the most common JSON error, particularly for developers coming from JavaScript where trailing commas are allowed in objects and arrays. The fix is to remove the comma after the last item in the object or array. Most JSON formatters highlight the exact line and position of the trailing comma.
Single quotes instead of double quotes is another frequent mistake. JavaScript developers often write JSON-like structures with single quotes because that is valid JavaScript object syntax. But JSON requires double quotes for both keys and string values. Use a JSON formatter to fix this correctly rather than a raw find-and-replace, which can break strings that contain apostrophes.
Unquoted keys are valid in JavaScript but invalid in JSON. The object literal {name: "value"} works in JavaScript but fails JSON.parse(). Wrap every key in double quotes. Similarly, undefined is not a valid JSON value (null is). If you are serializing JavaScript objects to JSON, use JSON.stringify() which handles these conversions automatically.
- Trailing comma: remove the comma after the last item in {} or []
- Single quotes: replace all single quotes with double quotes for keys and string values
- Unquoted keys: wrap every object key in double quotes
- Undefined: replace with null (JSON has no undefined)
- Boolean/null case: change True to true, False to false, None to null
How to Format JSON: Beautify vs Minify
Beautifying (also called formatting or pretty-printing) adds indentation and line breaks so each key-value pair is on its own line with consistent indentation showing the nesting depth. The standard indentation is two spaces or four spaces. Two spaces is more compact and works better for deeply nested structures. Four spaces is more readable at shallow nesting levels.
Minifying does the opposite: it strips all whitespace to produce the most compact valid JSON possible. All keys and values remain but every unnecessary space, newline, and tab is removed. Minified JSON is smaller in bytes, which matters for API payloads transferred over a network and for config files embedded in compiled code.
The rule of thumb: use beautified JSON when a human needs to read or edit it (in documentation, in tickets, in log review). Use minified JSON when a machine needs to parse it at scale (in API responses, in database storage, in bundled app config). Both are functionally identical because a JSON parser accepts either format.
How to Validate JSON in Different Languages
In JavaScript and Node.js, the simplest validation is a try-catch around JSON.parse(). If the input is valid JSON, parse() returns the object. If it is invalid, it throws a SyntaxError with a message that includes the position of the error. For production code, always validate untrusted JSON from user input or external APIs before using the parsed result.
In Python, use json.loads() inside a try-except block that catches json.JSONDecodeError. The error includes the line and column of the syntax problem. In Go, use json.Unmarshal() and check the error return value. For quick ad-hoc validation in the terminal, the jq command-line tool is the fastest option: pipe or pass the file to jq and if valid JSON it pretty-prints it, otherwise it shows the parse error with line and column.
For quick one-off validation in the browser, an online JSON formatter handles it in one click. Paste the JSON, click Validate, and the tool either shows the formatted output (valid) or the syntax error with the position (invalid).
JSON vs JSON5 vs JSONC: What Is the Difference
Standard JSON (RFC 8259) is the format accepted by JSON.parse() and all standard library parsers. It is strict by design: no comments, no trailing commas, no single quotes. This strictness makes it universally parseable across all languages and platforms.
JSON5 is a superset of JSON that adds JavaScript-style conveniences: single quotes, trailing commas, comments, unquoted keys, and multiline strings. JSON5 is useful for config files that humans edit frequently, but it requires a dedicated parser and will fail in any context that expects standard JSON.
JSONC (JSON with Comments) is used by VS Code for its settings.json file. It looks like JSON with double-slash comments, but it is not valid standard JSON. VS Code strips the comments before parsing. Use standard JSON when writing data for APIs or inter-service communication. JSON5 or JSONC are only appropriate for human-maintained config files where the parser is under your control.
The Fastest Tools for JSON Formatting and Validation
For quick one-off formatting in the browser, an online JSON formatter is the fastest option. Paste the JSON, click Format, and copy the result. The ShortIQ JSON formatter formats, minifies, and validates JSON in one click and runs entirely in your browser, so nothing is uploaded to a server.
For terminal work, jq is the standard. It formats, queries, and transforms JSON from the command line. The command jq . data.json pretty-prints the file with two-space indentation. jq is fast enough to process multi-gigabyte JSON files and is worth installing as a permanent dev tool (available on macOS via Homebrew and Linux via apt).
For IDE integration, every major editor has a JSON format-on-save option. In VS Code, set editor.formatOnSave to true and VS Code will automatically format JSON files each time you save. Combine this with an .editorconfig that specifies indent_size = 2 to keep all JSON files in the project consistently formatted without any per-developer configuration.
FAQ
What is a JSON formatter?
A JSON formatter takes compact or unformatted JSON and adds indentation and line breaks to make it human-readable. It also validates the JSON for syntax errors. If the input is invalid JSON, the formatter shows an error message with the location of the problem instead of formatting it.
What is the correct indentation for JSON?
JSON has no required indentation. Any amount of whitespace is valid as long as the structure is syntactically correct. The two most common conventions are two spaces and four spaces per indent level. Two spaces is more popular in JavaScript projects (Prettier defaults to two spaces). Four spaces is common in Python and some Java codebases. Pick one and enforce it consistently.
Why does my JSON have a syntax error?
The most common causes are: a trailing comma after the last item in an object or array, using single quotes instead of double quotes, an unquoted object key, or a boolean or null value with incorrect capitalization (True instead of true, None instead of null). Paste the JSON into a formatter to see the exact line and type of error.
Can JSON have comments?
No. Standard JSON does not support comments. JSON.parse() will throw a SyntaxError if the input contains single-line or block comments. If you need comments in a config file, use JSON5 or JSONC with a parser that supports them, or use YAML, which supports comments natively and is commonly used for config files.
What is the difference between pretty-print and minified JSON?
Pretty-printed (beautified) JSON has indentation and line breaks added for human readability. Minified JSON has all whitespace removed to produce the smallest possible valid JSON. Both contain exactly the same data and can be parsed by any standard JSON parser. Use pretty-printed JSON for documentation and debugging. Use minified JSON for API responses and storage where file size matters.
How do I validate JSON in Node.js?
Wrap JSON.parse() in a try-catch block. If the input is valid JSON, parse() returns the object. If it is invalid, it throws a SyntaxError. For example: try { const data = JSON.parse(input); } catch (e) { console.error(e.message); }. This pattern works in both Node.js and browser JavaScript.
How do I format JSON from the command line?
Use the jq command-line tool: cat data.json | jq . where the dot is jq shorthand for identity (output the input unchanged but formatted). jq is available on macOS via Homebrew (brew install jq) and on Linux via apt (sudo apt install jq). You can also use Python: python3 -m json.tool data.json to pretty-print a file without installing anything extra.
Related free tools
If you want to turn this topic into action, use one of ShortIQ's free tools for campaign planning, UTM structure, or QR distribution.
Continue Reading
Explore more guides on link shortener SaaS strategy, Bitly alternatives, and white label link management.
Free newsletter
Get new guides in your inbox
We publish practical guides on dev tooling, prompt engineering, marketing workflows, and deployment. No fluff — straight to the point.
No spam. Unsubscribe any time.
Was this article helpful?
Tell us if this guide solved the problem or what was still missing. We use this to improve the blog and only follow up if you explicitly allow it.