Skip to main content
ToolMaple

JSON Formatter and Validator

Paste one-line JSON and get it neatly indented; syntax errors show the line number.

Last updated:

What a JSON formatter actually does

This page runs your text through the browser built-in JSON.parse() and prints the result back with JSON.stringify(). That matters more than it sounds, because it means the page is not guessing at your document with a regular expression. Either the text is valid JSON and you get an indented copy of exactly the data a real parser saw, or it is not and you get the parser error, at the position where it gave up. Formatting and validating are the same operation here.

The reason the naive approach fails is that JSON is far stricter than the JavaScript or Python it looks like. The grammar, fixed in RFC 8259 and ECMA-404, allows exactly six kinds of value: object, array, string, number, the literals true and false, and null. Strings must use double quotes, keys are strings and therefore quoted too, there are no comments, no trailing commas, no single quotes, no hexadecimal, and no NaN or Infinity. A document that your editor highlights happily can still be rejected by every server you send it to.

Whitespace between tokens carries no meaning in JSON, so formatting and minifying are two views of one document rather than two different documents.

Formatted, minified or tree: which output you want

  • Format with 2 spaces: the default, and the convention most web tooling emits. Use it for anything that will end up in a repository, since a file that matches what npm, Prettier and your teammates produce keeps diffs small.
  • Format with 4 spaces: the house style in plenty of backend and mobile codebases, and easier to follow when the nesting is deep. Match the file you are pasting into rather than your own taste.
  • Minify: strips every insignificant space and newline. Use it when the JSON has to travel as one line: a shell argument, an environment variable, a database column, or a config field that accepts only a single line.
  • Tree view: for reading rather than producing. Every value carries a type badge, so you can see at a glance that the field you expected to be a number arrived as a string, and containers show how many children they hold. Clicking a key copies its JSONPath.
  • Validation only: press Format and look for the red box. No error means the document parsed cleanly.

How to read an API response you just captured

  1. Copy the response body. In Chrome or Firefox DevTools that means the Network panel: select the request, open the Response tab and copy the raw text.
  2. Paste it into the box above and press Format. If a red box appears, you probably copied the headers along with the body, or the endpoint returned an HTML error page rather than JSON.
  3. Switch to Tree view and collapse the branches you do not care about. The type badges are the fastest way to spot the usual API surprises: an ID sent as a string, a boolean sent as "false", an empty list that arrived as null.
  4. Click the key you need. The JSONPath lands on your clipboard, ready for jq, a test assertion or a line of code that has to walk to that field.

How to fix JSON that refuses to parse

Read the position in the error, put your cursor there, then look at the few characters before it. Parsers report where they noticed the problem, not where you made it. These are the offenders, in rough order of how often they show up:

  1. Trailing comma: {"a": 1, "b": 2,} is fine in modern JavaScript and invalid in JSON. Delete the last comma.
  2. Single quotes or unquoted keys: {a: 'x'} is a JavaScript object literal, not JSON. It has to be {"a": "x"}.
  3. Comments: remove every // and /* */. If the file is meant to keep them, it is JSONC or JSON5, not JSON.
  4. Curly quotes: text that passed through a word processor, a chat app or a slide deck often comes back with and in place of ". Retype the quotes.
  5. Unescaped characters inside strings: a literal double quote has to be \", a newline \n, a backslash \\. A Windows path pasted in raw is the classic case.
  6. An invisible first character: a byte order mark left by a Windows editor sits before the opening brace and makes the parser fail at position 0. Delete everything ahead of the { and paste again.
  7. Python or log output rather than JSON: True, None and 'single quotes' mean you copied the printed form of a dict. Print it with json.dumps() instead.

Getting the result where it needs to go

The Copy button above the result copies the whole formatted document; in the tree view, clicking a key copies only its path.

  • Into an editor: paste the formatted version, not the minified one. Your reviewer reads the diff, and a file that is one very long line shows up as a single changed line no matter how small the edit was.
  • Into a shell: minify first, then wrap the whole thing in single quotes so the shell leaves the double quotes alone, as in curl -d '{"id":1}' -H 'Content-Type: application/json'. On Windows PowerShell the quoting rules differ, so a here-string or a file is safer than an inline literal.
  • Into an environment variable or a single-line config: minify, and remember that a JSON value containing a newline can only be stored as \n inside a string.
  • On iPhone or Android: use the Copy button rather than a manual selection, since dragging a selection across several screens of text is painful on a phone.
  • Sharing with someone else: strip tokens, keys and personal data first. What you paste here stays in your browser, but what you forward afterwards does not.

The same document in other languages and stores

Every runtime has its own formatter, and they do not all agree on what they will accept or emit.

  • JavaScript and TypeScript: JSON.stringify(value, null, 2) is what this page does. Pass a string as the third argument for tabs, as in JSON.stringify(value, null, '\t'). A numeric indent above 10 is clamped to 10 by the specification.
  • Python: json.dumps(obj, indent=2), or python -m json.tool file.json from a terminal. Two defaults catch people out: ensure_ascii escapes non-ASCII text to \uXXXX unless you pass ensure_ascii=False, and the module accepts and emits NaN and Infinity, neither of which exists in JSON. Pass allow_nan=False for portable output.
  • Command line: jq . pretty-prints and jq -c . compacts, both streaming, which is how to handle a file too large for a browser tab.
  • PostgreSQL: the json type keeps your exact text, including whitespace, key order and duplicate keys. The jsonb type parses into a binary form, so whitespace disappears, key order is not preserved and duplicate keys are collapsed. jsonb_pretty() gives you an indented rendering back. Pick jsonb unless you genuinely need the original bytes.
  • Log pipelines: a file with one JSON document per line, often called JSON Lines or NDJSON, is not a single JSON document and will not parse here as a whole. Paste one line at a time.
  • Editors: VS Code accepts comments and trailing commas in tsconfig.json and its own settings files because it treats them as JSONC. That tolerance does not extend to package.json, which npm parses strictly.

Limits, privacy and honest caveats

What leaves your browser: nothing. Parsing and printing both happen on your device through the browser JSON functions. The text you paste is not sent anywhere, not logged and not stored on a server, and the page keeps no copy once you close the tab. Session recording on this site is configured to mask this tool, so the pasted document is not captured there either.

Numbers are the one place data can change. A JavaScript parser turns every JSON number into a double, so integers beyond 9007199254740991 are rounded and long decimal values can come back with a different tail. If your payload carries large IDs or money amounts, read them from the raw text and have the producer send them as strings.

Two more things a round trip can quietly alter. Duplicate keys collapse to the last occurrence, which RFC 8259 warns is parser-dependent, and keys that look like non-negative integers are reordered ahead of the rest because that is how JavaScript objects enumerate. When either detail matters, treat the output as a view rather than a replacement for the original file.

Size and accessibility. Everything runs in one browser tab, so a very large document can freeze it, and the tree view is the heavier mode because it builds a node per value. Reach for a streaming tool past a few megabytes. The type badges are colored but also labeled in text, so nothing is carried by color alone.

This is syntax checking, not schema validation. A document can be perfectly valid JSON and still be wrong for the API you are sending it to. For required fields, types and allowed values, you need a JSON Schema validator or the documentation for the service itself.

Frequently asked questions

Why does my JSON fail to parse when it looks correct?

Almost always one of five things: a trailing comma before a closing brace or bracket, single quotes instead of double quotes, keys written without quotes, a // or /* */ comment, or curly quotes pasted in from a word processor or a chat app. All five are legal in JavaScript source or in a Python dict, and none of them are legal JSON. The red box under the input repeats whatever your browser engine reported, which normally includes the character position and, in current Chrome and Firefox, the line and column as well. Start reading there and work backwards a line or two, because the real mistake is usually just before the point where the parser gave up.

What is the difference between formatting and minifying?

Nothing but whitespace. Formatting adds newlines and indentation so a human can follow the nesting; minifying strips every space and newline between tokens so the payload is as small as possible. The parsed data is identical either way, which is why you can round-trip a document through both buttons and get back exactly what you started with. Send minified JSON over the wire and read formatted JSON on screen.

Is my JSON uploaded to a server?

No. The page calls the browser built-in JSON.parse and JSON.stringify on your own device, and the text you paste never leaves the tab. There is no request carrying your input, no logging of it, and nothing is kept after you close the page. That makes it safe to paste an API response that contains tokens or customer records, although it is still worth redacting anything you do not need before you paste.

Should I indent with 2 spaces or 4?

Two spaces is the common default for JSON in web projects and is what most formatters emit, including this page and the JSON that npm writes into package.json. Four spaces reads better when the nesting is shallow but the values are long, and some teams standardize on it across every file type. Pick whichever matches the repository you are pasting into, because a diff full of reindented lines hides the change you actually made.

Why did my long ID number change after formatting?

JavaScript parses every JSON number into a 64-bit floating point double, which holds integers exactly only up to 9007199254740991. A 19-digit snowflake ID or a bigint primary key is past that limit, so it is rounded on the way in and the rounded value is what gets printed back out. The fix is on the producing side: serialize large identifiers as JSON strings. If you only need to read the value, search the raw text rather than trusting the formatted output.

What happens if the same key appears twice in one object?

RFC 8259 says names within an object should be unique but does not forbid duplicates, and it warns that behavior with duplicate names is unpredictable across parsers. JSON.parse in the browser keeps the last occurrence and silently discards the earlier ones, so the formatted output will show only one of them. If you are debugging a payload where a value seems to vanish, search the raw text for the key before you assume the server never sent it.

Does formatting reorder my keys?

Usually no: ordinary string keys come back out in the order they appeared. The exception is keys that look like non-negative integers, such as "2" and "10". JavaScript objects always list integer-like keys first and in ascending numeric order, so a map keyed by ID will come back sorted rather than in the original order. The data is the same, but if key order matters to you, treat the raw text as the source of truth.

Can I put comments in a JSON file?

Not in JSON itself. The grammar has no comment syntax, so a // or /* */ line makes the document invalid and package.json or a strict API will reject it. Two supersets exist for the cases where humans edit the file by hand: JSONC, which adds comments and is what VS Code accepts in tsconfig.json and its own settings, and JSON5, which adds comments, trailing commas, unquoted keys and single quotes. Strip both back to plain JSON before sending anything over an API.

How do I get the path to a nested field?

Switch to the tree view and click the key you want. The page copies a JSONPath-style string such as $.data.items[0].id, built from the position of that node in the document. Bracket notation is used for array indices and for keys that are not plain identifiers, so a key with a space or a dash comes out as ["user-id"] rather than breaking the path. Paste that into jq, a JSONPath query or your own code.

How large a document can this handle?

It is limited by the memory of your browser tab rather than by any fixed cap, and the practical ceiling depends on your device. A few megabytes formats without trouble on a laptop; tens of megabytes can lock the tab up while it renders, and the tree view is heavier than the text view because it creates a DOM node per value. For log files and database dumps, a streaming command line tool such as jq is the better answer.

Sources

  • IETF, RFC 8259, The JavaScript Object Notation (JSON) Data Interchange Format (the grammar, UTF-8 as the interchange encoding, and the warning about duplicate object names).
  • MDN, JSON.parse() and JSON.stringify() (the two functions this page calls, including the indent argument and its limit of 10).
  • json.org (Douglas Crockford’s one-page railroad diagrams of the grammar, and the companion standard ECMA-404).
  • Behavior of the json and jsonb column types, of the Python jsonmodule, and of JSONC in VS Code is described in each project’s own documentation, which can change between versions.

More developer tools

A JSON payload rarely travels alone. If a field holds an opaque blob, try it in the Base64 decoder; three dot-separated segments usually mean a JWT, whose header and payload are themselves JSON. Query strings that carry escaped JSON are easier to read after a pass through the URL encoder and decoder, and when you need an identifier for a new record the UUID generator produces one that will not collide.

Related tools

Privacy: your JSON is parsed and printed by your own browser with JSON.parse() and JSON.stringify(). It is never uploaded, never logged and never stored on a server, and the tool is masked from session recording. Analytics sees only that this page was viewed, never its contents.