JSON Formatter

Format, validate, minify and sort JSON.

About this tool

Paste JSON to pretty-print it with your preferred indentation, minify it for transport, or sort keys alphabetically to make diffs readable. Syntax errors are reported with line and column.

How to use it

  1. Paste your JSON into the Input box, or paste it into the search with ⌘K and pick JSON Formatter.
  2. Choose 2 spaces, 4 spaces, tabs, or Minify to put everything on one line.
  3. Turn on Sort keys if you want keys in alphabetical order, which makes two versions easier to compare.
  4. Copy the result. If the JSON is invalid, the error shows the line and column to fix.

Questions

Why is my JSON invalid?
The usual causes are a trailing comma after the last item, single quotes instead of double quotes, unquoted keys, or comments. JSON allows none of these. The error message points to the exact line and column.
Can it read a Python dict or a JavaScript object?
Yes. Values with single quotes, True, False, None, unquoted keys or trailing commas are converted to valid JSON automatically, and the status line tells you it happened.
Is it safe to paste API responses with personal data?
Yes. Formatting happens in your browser and nothing is sent to a server, so you can paste production data without it leaving your machine.
What is the difference between formatting and minifying?
Formatting adds line breaks and indentation so people can read it. Minifying removes all optional whitespace so the payload is as small as possible for sending over the network.
What happens to duplicate keys in a JSON object?
The JSON standard doesn’t forbid duplicate keys, but it leaves their meaning open. Here, as with JSON.parse in every browser, the last value wins, so {"a": 1, "a": 2} formats to {"a": 2}. If a key seems to vanish after formatting, search the input for a second copy of it.
Can I format a large JSON file?
Yes. Click Open file or drop the file onto the Input box. It is read locally, never uploaded, and files up to 50 MB are accepted. Very large documents make the text box slower to scroll, so use Download to save the result as formatted.json instead of copying it.
How do I format JSON Lines or NDJSON?
JSON Lines puts one JSON value on each line, so the file as a whole is not valid JSON and the formatter reports an error where the second value starts. Format one line at a time, or turn the file into an array by wrapping it in [ ] and adding a comma at the end of every line but the last.

What is a JSON formatter and validator?

A JSON formatter takes compact or messy JSON and prints it again with consistent indentation and one key per line, so nested data becomes readable. A JSON validator checks that the text follows the JSON grammar: double-quoted keys and strings, no trailing commas, no comments, and only true, false and null as literals. This tool does both in one pass. Valid input comes back beautified, and invalid input shows the parser’s message with a line and column.

Beautifying JSON never changes the data. Keys, values and array order stay exactly as they were, and only whitespace changes. One limit is worth knowing: JSON numbers are read as JavaScript doubles, so integers above 9007199254740991 lose precision. An ID like 12345678901234567890 comes back as 12345678901234567000. If an API sends IDs that large, ask for them as strings, which is what many APIs already do for 64-bit IDs.

How to fix “Unexpected token” in JSON

“Unexpected token” means the JSON parser hit a character that can’t appear at that point. Read the line and column in the error and look at the text just before that spot. The usual culprits are a trailing comma as in [1, 2, ], a key without quotes, a string in single quotes, a // comment copied from a config file, or a value like NaN or Infinity that JavaScript prints but JSON doesn’t allow.

The error “Unexpected token < in JSON at position 0” has a different cause: the text is an HTML page, usually an error page or a login redirect, not the API response you expected. Check the HTTP status and the Content-Type header before parsing. When the input is a Python dict or a JavaScript object literal, the JSON Formatter repairs single quotes, True, False, None, unquoted keys and trailing commas, and lists each fix in the status line.

Should you sort JSON keys? Indentation and sorting best practices

Key order has no meaning in a JSON object, so sorting keys alphabetically is safe and makes two versions easy to compare. Sort keys works recursively: nested objects are sorted too, while arrays keep their order, because position matters inside an array. Sort both versions, then paste them into Text Diff, and only real value changes show up instead of keys that merely moved to another place.

For JSON indentation, 2 spaces is the most common choice in JavaScript projects and API documentation, 4 spaces matches the habit of json.dumps(indent=4) in Python, and tabs let every reader pick their own width. Minified JSON belongs in network payloads, environment variables and log lines, where one record per line matters. Whatever you choose, keep it consistent across a repository so reformatting doesn’t clutter code review.

Something not working, or a result looks wrong? Report a problem with this tool