JSON (JavaScript Object Notation) is the most common data format in REST APIs, config files and lightweight storage. Knowing how to format, minify and validate JSON saves time when debugging API responses or reviewing config files.
Format vs minify
- Format (pretty-print): add indentation and newlines so the JSON is readable. Essential when debugging API responses or checking
package.json,tsconfig.json, etc. - Minify: remove spaces and newlines to reduce size. Used in production when bandwidth matters or when embedding JSON in HTML/JS.
An online JSON formatter usually offers both: “Format” to read and “Minify” to shrink, plus validation that reports the line of the first error if the text isn’t valid JSON.
Common JSON mistakes
- Trailing comma:
{"a": 1,}is invalid; the last property must not be followed by a comma. - Quotes: keys and strings must use double quotes
", not single'. - Unquoted values: strings must be in quotes; numbers and booleans must not.
- Comments: JSON doesn’t support
//or/* */; remove them before validating or use a format that allows them (e.g. JSONC where supported).
A JSON validator returns the message and often the position of the error so you can fix it quickly.
Use in APIs and development
When consuming an API, the response is usually JSON. Pasting it into a formatter shows the structure clearly and helps locate fields. In your own code, produce JSON with JSON.stringify() and in development use the third argument for indentation (e.g. JSON.stringify(obj, null, 2)). For production, omit indentation if size matters. In short: format to read and debug, minify when size matters, and always validate when the source isn’t trusted.