Converter tool

JSON Formatter and Validator

Format JSON with 2 spaces, 4 spaces or tabs, minify it to one line, or validate it and see the line and column of the first error. Runs in your browser.

Pasted text0 B / 5 MB

Preview

Output Preview

Paste JSON or choose a file, then press Format, Minify or Validate.

JSON Formatter and Validator

Operator: Abdulla Al Fahad. How tools are built and checked

An API answer arrives as one 40,000-character line, or a config file refuses to load with nothing more than "unexpected token". This tool handles both: Format spreads JSON over indented lines so the nesting is readable, Minify squeezes it back into one line, and Validate tells you whether the text is valid JSON and, if not, the line and column where parsing stopped.

It also points out two quiet problems that a plain parser lets through: the same key written twice in one object, and numbers too large to survive being read into JavaScript. Both are common in real data and both change the output without raising an error, so they are listed as warnings above the result.

Key features

  • Format with 2-space, 4-space or tab indentation.
  • Minify to a single line with no whitespace.
  • Validate with the line and column of the first error and the faulty line shown with a caret under the problem.
  • Warnings for duplicate keys and for numbers that cannot be represented exactly.
  • Optional sorting of object keys at every level, with arrays left in their original order.
  • Copy the output to the clipboard or download it as a .json file.
  • Paste text or open a .json or .txt file up to 5 MB.

What JSON allows and how it is re-printed

JSON allows six kinds of value: objects in braces, arrays in square brackets, strings in double quotes, numbers, true, false and null. Keys must be double-quoted strings, there is no trailing comma after the last item, and comments are not allowed. Whitespace between tokens is free, which is why the same data can be one line or a thousand.

Formatting re-prints the parsed value with an indent of 2 spaces, 4 spaces or a tab per level. Minifying prints it with no whitespace at all. Either way the text is first parsed, so the output is the browser's own serialisation: escapes such as \u00e9 become the plain character é, and a number written as 1.0 or 1e3 comes out as 1 or 1000.

Text input and files are limited to 5 MB. Files must be UTF-8 text; a leading byte order mark is accepted and dropped, with a warning, because several strict parsers reject it.

How it works

Each action hands the text to the browser's built-in JSON.parse. If parsing succeeds, the value is re-printed with JSON.stringify using the chosen indent, after an optional pass that rebuilds every object with its keys in sorted order.

If parsing fails, the tool reads the error position out of the browser's message when it has one. It also runs a small scanner that walks the same grammar character by character and stops at the first fault, which gives the position in browsers that do not report one and supplies the explanation shown under the browser's message. The same scanner runs on valid input to spot duplicate keys and oversized numbers.

Common use cases

  • Reading a minified response copied from the browser's network panel to find one field deep inside it.
  • Finding the missing comma in a package or app config file after a hand edit.
  • Minifying a fixture before pasting it into a test, an environment variable or a query string.
  • Sorting the keys of two exported settings files so they line up in a text diff.
  • Checking that a payload built by hand for a webhook test is valid before sending it.

How to use this tool

  1. Paste JSON into the input box, or press Choose file and pick a .json file.
  2. Pick the indent you want and decide whether to sort keys.
  3. Press Format to pretty-print, Minify for one line, or Validate to check only.
  4. If there is an error, go to the line and column shown and fix the text in the box, then run it again.
  5. Press Copy output or Download .json to take the result.

Example

A config file with a missing comma after the port value

{
  "service": "billing",
  "port": 8080
  "debug": false
}

Validate result

Invalid JSON at line 4, column 3
Expected a comma or } after a property value, found a double quote. A comma is probably missing before it.
  "debug": false
  ^
(The comma is missing at the end of line 3; the parser only notices when line 4 starts a new key.)

Tips for best results

  • Validate first when a file will not load. The status line also tells you what the top level is, for example an array of 250 items, which catches a file that was saved as one object instead of a list.
  • When the reported position is the start of a line, look at the end of the previous line. A missing comma is only noticed when the next key begins.
  • Leave Sort object keys off when key order carries meaning to a human reader, such as a manifest that lists name and version first.
  • Use the tab indent if the file will be edited in a project whose editor settings use tabs; it keeps the file consistent with its neighbours.
  • Drop a .json file onto the input box to load it without opening it in an editor first; the file name is then used for the download.

Common pitfalls

Big integers are rounded. JavaScript stores every number as a 64-bit double, which holds integers exactly only up to 9,007,199,254,740,991. An order ID of 9007199254740993 is printed back as 9007199254740992. The tool warns when it sees such a number; the fix is for the producer to send those IDs as strings.

Duplicate keys collapse to the last value. {"status":"paid","status":"refunded"} formats as just "status": "refunded". The JSON standard leaves duplicate keys undefined, so other parsers may keep the first value instead; the warning lists the repeated names so you can fix the source.

This is strict JSON, not JSON5 or JSONC. Comments, single-quoted strings, unquoted keys and trailing commas, all of which some config files accept, are reported as errors.

The error message text depends on the browser. Chrome, Firefox and Safari each word their parser errors differently, and Safari's messages carry no position. The line and column shown here come from the browser where it gives one and from the tool's own scanner otherwise, and the plain-language hint under the message is always from the scanner.

Common errors and how to fix them

There is a trailing comma before } or ].
Delete the comma after the last item. JSON, unlike JavaScript, does not allow it.
Expected a property name in double quotes, found a single quote.
Replace the single quotes around keys and string values with double quotes. A find-and-replace is risky if the text contains apostrophes, so fix the quotes around keys first.
A string contains a raw control character.
A real line break or tab sits inside a string value, often from text pasted out of a document. Replace it with \n or \t.
There is extra text after the end of the JSON value.
The input holds more than one top-level value, for example two objects one after another or a JSON Lines file. Wrap them in [ ] with commas between, or format one line at a time.
The file is rejected as not valid UTF-8 text.
It was saved in another encoding, such as Windows-1252 or UTF-16. Open it in a text editor and save it again as UTF-8.

Best practices

  • Keep the minified form for transport and the formatted form for files people edit and review.
  • Treat a duplicate-key warning as a bug in whatever produced the file, not just a formatting issue.
  • Store identifiers longer than 15 digits as strings in any JSON you design.
  • Sort keys only in copies made for comparison, so the original order in your source files stays meaningful.

Why the error is often one line below the mistake

A parser reads left to right and only fails when the next character cannot fit. After "port": 8080 a comma or a closing brace is still possible, even across a line break, so nothing is wrong yet. The failure happens at the double quote that opens "debug" on the next line, and that is the position reported. The same applies to an unclosed string: the error lands at the end of the input, far from the opening quote.

So read the reported position as the place where the parser gave up, then look just before it: the end of the previous line, the previous closing bracket, or the last quote.

How line and column are counted

Lines start at 1 and each line break ends a line, whether it is a Unix line feed, a Windows carriage return plus line feed, or an old Mac carriage return. Columns start at 1 at the first character of the line and count UTF-16 code units, the same unit JavaScript uses, so an emoji in a string before the error moves the column by 2. Tabs count as one column each, even though editors draw them wider.

For a minified file everything is on line 1, so the column can run into the tens of thousands. The faulty line is clipped to about 60 characters either side of the error with the caret underneath, so the context stays readable.

Frequently asked questions

Does formatting change my data?

The values stay the same with two exceptions covered by the warnings: integers beyond the exact range are rounded, and duplicate keys keep only their last value. Number spelling (1.0 versus 1) and string escapes are normalised by the browser.

Can it format JSON Lines or NDJSON?

Not as a whole file, because each line is a separate JSON value and the file as a whole is not valid JSON. Format one line at a time, or wrap the lines in an array first.

Are comments in my config file kept?

No. Comments are not part of JSON, so a file containing them fails validation. Remove them, or keep the commented file as your source and format a copy without comments.

Why does sorting put uppercase keys before lowercase ones?

Keys are sorted by character code, the same order most programming languages use by default, and in that order every uppercase letter comes before every lowercase one. "Zone" therefore sorts above "apple". Keys that are whole numbers are the exception: JavaScript always lists them first, in numeric order, so {"b":1,"10":2,"2":3} sorts to "2", "10", "b".