How to Pretty-Print, Minify, and Validate JSON Online (and in Code)
These three operations get lumped together but solve different problems: pretty-printing makes JSON readable for a human, minifying strips whitespace to shrink it for a network response, and validating checks whether it's even syntactically correct JSON in the first place. Here's how to do each, both with quick browser tools and in code.
Pretty-Printing (Formatting for Readability)
A minified API response like {"id":1,"name":"Ana","tags":["vip","new"]} is unreadable at scale. Pretty-printing adds indentation and line breaks so nested structure is visible at a glance.
In the browser console or Node:
JSON.stringify(data, null, 2)
// the "2" is the indent width in spacesFrom the command line, using jq:
curl https://api.example.com/users | jq '.'jq is worth installing regardless — beyond pretty-printing, it lets you filter, map, and reshape JSON directly in a pipeline, which is faster than writing a throwaway script for a one-off inspection.
Minifying (Stripping Whitespace for Production)
Pretty-printed JSON wastes bytes over the wire — every space and newline is transmitted. Minifying removes them without changing the data, which matters for large payloads or high-traffic APIs where every byte affects latency and bandwidth cost.
JSON.stringify(data)
// no third argument = no indentation, fully minifiedjq -c '.' input.json > output.min.jsonIn practice, most HTTP servers handle this automatically as part of serialization — you rarely need to minify manually unless you're pre-generating static JSON files served directly from a CDN or file system.
Validating (Checking It's Actually Valid JSON)
Validation catches syntax errors — a trailing comma, an unescaped quote, a missing bracket — before they crash a parser downstream. The fastest check is trying to parse it and catching the error:
function isValidJson(str: string): boolean {
try {
JSON.parse(str)
return true
} catch {
return false
}
}For validating not just syntax but structure — required fields, correct types — you need JSON Schema validation instead, using a library like Ajv rather than a plain try/catch:
import Ajv from 'ajv'
const ajv = new Ajv()
const validate = ajv.compile(schema)
if (!validate(data)) console.log(validate.errors)Common JSON Syntax Mistakes That Fail Validation
- Trailing commas:
{"a": 1, "b": 2,}— valid in JavaScript object literals, invalid in strict JSON. - Single quotes: JSON requires double quotes for both keys and string values; single quotes are a JavaScript-only convenience.
- Unquoted keys:
{name: "Ana"}is valid JS, invalid JSON — keys must be quoted strings. - Comments: JSON has no comment syntax at all —
//or/* */inside a JSON file will fail to parse (JSON5 and JSONC support comments, but they aren't standard JSON).
When You Need Sample Data to Test All This
If you're building or testing a JSON formatter, validator, or minifier yourself, you'll want varied sample payloads — deeply nested objects, large arrays, edge-case types like null and empty strings. Dummy JSON Generator is a fast way to produce that variety without hand-writing each test case.
The Bottom Line
Pretty-print for humans, minify for production payloads, and validate before trusting any JSON you didn't generate yourself — three distinct operations, each with a one-line solution once you know which one you actually need.