DevToolbox
All articles

JSON Formatting and Validation: A Complete Developer Guide

· 7 min read

JSON (JavaScript Object Notation) is the universal language of modern APIs, configuration files, and data interchange. It is simple by design — yet a single misplaced comma or an unquoted key can bring an entire application to a halt. This guide covers everything you need to know about formatting, validating, and working confidently with JSON, whether you are debugging an API response at midnight or preparing a config file for production.

What Is JSON Formatting and Why Does It Matter?

Raw JSON returned by an API is often a single, dense line with no whitespace. That is fine for machines, but it is nearly unreadable for humans. JSON formatting — also called pretty-printing or beautifying — adds consistent indentation and line breaks so the structure of objects and arrays is immediately visible.

Formatting matters for three practical reasons. First, it dramatically speeds up debugging: nested structures, unexpected null values, and missing fields are obvious in formatted JSON and invisible in minified blobs. Second, it helps during code review — diffs of formatted JSON show exactly which fields changed. Third, many editors and linters expect consistently formatted JSON in config files like package.json or tsconfig.json.

Valid JSON Syntax Rules

JSON has a surprisingly strict grammar compared to the JavaScript object literals it resembles. The full spec is defined at json.org, but the rules developers most often forget are:

  • All string keys must be double-quoted. { name: "Alice" } is not valid JSON. It must be { "name": "Alice" }.
  • All string values must use double quotes. Single quotes are not permitted anywhere in JSON.
  • No trailing commas. [1, 2, 3,] and { "a": 1, } are both syntax errors. The last item in an array or object must not be followed by a comma.
  • No comments. // line comments and /* block comments */ are JavaScript features, not JSON. Many developers expect comments to work in JSON config files — they do not in strict JSON (though some tools like JSON5 or JSONC support them as extensions).
  • Numbers cannot have leading zeros. 007 is not a valid JSON number. Use 7.
  • Only these value types are allowed: string, number, object, array, true, false, null. undefined, NaN, Infinity, and Date objects are all invalid JSON values.

Pretty-Print vs. Minify — When to Use Each

Pretty-printing adds indentation (typically 2 or 4 spaces) and newlines to make JSON human-readable. Use it in version-controlled config files, during development, and whenever humans need to read or review the data. The readability benefit far outweighs the extra bytes when the file is not on a hot data path.

Minifying removes all whitespace and produces the most compact valid representation. Use it for API responses, JSON embedded in HTML, and any context where JSON is consumed only by machines and every byte counts. A minified payload can be 20–40% smaller than its pretty-printed equivalent, which adds up across thousands of API calls.

The rule of thumb: pretty-print for humans, minify for machines. Your build pipeline should handle the conversion automatically — author config files in pretty-printed form, then minify during deployment if needed.

Common JSON Syntax Errors (and How to Fix Them)

The following errors account for the vast majority of JSON parse failures in real codebases.

Missing comma between object properties:

// Invalid
{
  "name": "Alice"
  "age": 30
}

// Valid
{
  "name": "Alice",
  "age": 30
}

Trailing comma after the last item:

// Invalid
{
  "name": "Alice",
  "age": 30,
}

// Valid
{
  "name": "Alice",
  "age": 30
}

Unquoted keys:

// Invalid (JavaScript object literal syntax, not JSON)
{ name: "Alice" }

// Valid
{ "name": "Alice" }

Single-quoted strings:

// Invalid
{ "name": 'Alice' }

// Valid
{ "name": "Alice" }

Comments inside JSON:

// Invalid
{
  // user record
  "name": "Alice"
}

// Valid — remove the comment entirely
{
  "name": "Alice"
}

How to Validate JSON in the Terminal

You do not always have access to a browser or GUI tool. These command-line options cover the most common environments.

Python (built into macOS, Linux, and most CI images):

# Validate and pretty-print in one step
python3 -m json.tool data.json

# Validate a piped API response
curl -s https://api.example.com/data | python3 -m json.tool

jq (the Swiss-army knife of JSON processing):

# Install on macOS
brew install jq

# Validate and pretty-print
jq . data.json

# Check exit code without output (useful in scripts)
jq . data.json > /dev/null && echo "Valid" || echo "Invalid"

Node.js (available on any developer machine):

node -e "JSON.parse(require('fs').readFileSync('data.json','utf8')); console.log('Valid')"

All three approaches print a clear error message including the line number when the JSON is invalid, which makes them far more useful than just catching an exception in application code.

Tips for Working with Large JSON Files

Pretty-printing a 50 MB JSON file and loading it into a browser tab can freeze your machine. Here are practical strategies for large files:

  • Use streaming parsers in code. In Node.js, the stream-json package parses JSON without loading the entire file into memory. Python's ijson library does the same.
  • Use jq for exploration. jq '.users[0]' extracts just the first user without materialising the whole structure. jq 'keys' lists top-level keys instantly.
  • Validate before pretty-printing. Run a quick syntax check first. There is no point formatting a 100 MB file only to find a parse error on line 2.
  • Use a tool with virtual scrolling. Desktop editors like VS Code handle large files well because they only render the visible portion. Browser-based formatters can struggle — look for ones that implement virtual scrolling.
  • Split the file. jq '.items[0:1000]' extracts the first 1,000 items into a smaller, manageable file for inspection.

Format and Validate JSON Online

For quick one-off formatting tasks, an online tool is often the fastest option. The DevToolbox JSON Formatter runs entirely in your browser — your data never leaves your machine. It supports pretty-printing with configurable indentation, minifying, syntax error highlighting with line numbers, and virtual scrolling for large files. Paste your JSON, see the result instantly.

Summary

JSON's simplicity is also its strictness: double-quoted keys, no trailing commas, no comments, no undefined values. Formatting makes JSON readable during development; minifying makes it efficient in production. When you hit a parse error, the fastest path to a fix is a validator that tells you the exact line number — whether that is python3 -m json.tool in your terminal, jq, or an online formatter. Getting comfortable with these tools will save you hours across a career of debugging API responses and config files.

Try it free
JSON Formatter
100% client-side · no signup · no upload
Open tool →