Home / Code & Data / JSON Formatter
04.01Code & Data

JSON Formatter & Validator

Paste JSON to pretty print, minify or validate it. Errors come with a plain English explanation and the exact line and column, and a tree view makes deep API responses easy to read.

Updated

The JSON rules that trip people up

JSON looks like JavaScript, and that is exactly why it breaks. It is a much stricter subset, defined in RFC 8259, and parsers are not allowed to guess what you meant. These are the mistakes that cause almost every "Unexpected token" error:

  • Single quotes. Strings and keys must use double quotes. {'a': 1} is invalid.
  • Unquoted keys. {name: "Ana"} works in JavaScript and fails in JSON.
  • Trailing commas. [1, 2, 3,] is invalid, and so is a comma after the last property.
  • Comments. Neither // nor /* */ is allowed. If you need comments in config, use JSONC or JSON5 and a parser that supports it.
  • Special values. undefined, NaN and Infinity do not exist in JSON. Use null.
  • Raw line breaks in strings. A newline inside a string must be written as \n.

When this validator finds a problem it reports the rule, the line and column, and shows the line with a caret under the culprit. Click Jump to error to put your cursor there.

Formatting, minifying and sorting

Pretty printing with two spaces is the convention in most JavaScript projects, four spaces is common in Python and PHP codebases, and tabs keep files small while letting each developer pick their own width. Minifying strips every optional space and line break, which is what you want for API payloads, data- attributes and anything you embed in HTML.

Sorting keys is a review tool. Two API responses with the same data in a different order look completely different in a diff. Sort both, and only the real changes remain. Arrays keep their order because array order has meaning.

Working with JSON from the command line

The same operations are one-liners once you know them, which is handy in build scripts:

# pretty print and sort keys
jq --sort-keys . response.json

# minify
jq -c . response.json > response.min.json

# validate in Node without installing anything
node -e "JSON.parse(require('fs').readFileSync('data.json','utf8'))"

Big numbers and IDs

Every JavaScript JSON parser, including the one in your browser, turns numbers into 64-bit floats. Integers above 9007199254740991 get silently rounded, which is a classic bug with Twitter style snowflake IDs and some database keys. This tool warns you when your input contains numbers that cannot survive the round trip. The fix belongs on the server: send large IDs as strings.

Reading the tree view

The tree view is built lazily, so even a multi-megabyte response opens instantly. Each object shows its key count and each array its length before you expand it, which makes it quick to find the one nested field you care about in a large webhook payload.

Frequently asked questions

Is my JSON sent to a server?

No. Parsing, formatting and validation all happen in your browser with JavaScript. You can disconnect from the internet after the page loads and it keeps working.

Why is my JSON invalid when it works in JavaScript?

JavaScript object literals allow single quotes, unquoted keys, trailing commas and comments. JSON allows none of these. The validator names the exact rule you broke and where.

Why did a large number change after formatting?

JavaScript numbers are 64-bit floats, so integers above 9,007,199,254,740,991 lose precision when parsed. The tool warns you when this happens. Store big IDs as strings to keep them exact.

Does sorting keys change the meaning of my JSON?

No. Object key order has no meaning in JSON. Sorting only makes files easier to diff and review. Array order is never changed.