utilobox

JSON formatter and validator: read, check and fix your JSON

A response from an API lands as one unreadable line, or a config file fails with an “unexpected token” error. This guide shows how to use a JSON formatter and validator to pretty print the data, find the exact spot of an error and fix the mistakes that come up most often.

Last updated:

JSON formatter and validator: what you get from one paste

Paste JSON into the JSON formatter and validator and it is checked as you type. If the text is valid, the status shows ‘Valid JSON’, a few figures appear (‘Input size’, ‘Output size’, ‘Keys’ and ‘Depth’), and the formatted text appears in ‘Result’. If it is not valid, the result stays empty with the note ‘Fix the error above to see the result’, and you get a message with the line, the column, a short reason and an excerpt of the faulty line with a caret under the exact character.

The tool is strict on purpose. It checks against the JSON standard, not against what a forgiving program might accept. That is the right behavior for a validator: if your data passes here, other strict readers should accept it. It is not a repair tool, though. It tells you where the problem is and does not rewrite the text for you.

Pretty print JSON, step by step

With a JSON formatter you start by pasting the text into ‘JSON input’. Use ‘Paste’ to take it straight from the clipboard, or ‘Example’ to see a sample with nested data. Under ‘Output’ choose ‘2 spaces’, ‘4 spaces’ or ‘Tab’ for readable output with one item per line, or ‘Minified’ for a single compact line.

Use ‘View’ to switch between ‘Text’, a colored listing, and ‘Tree’, a collapsible outline. Then press ‘Copy’, or ‘Download’ to save the result as data.json. ‘Use as input’ moves the formatted text back into the input box, which is handy when you want to minify what you just formatted, or sort it and start again. ‘Clear’ empties the input.

Pretty printing never changes the data, only the white space between the parts. Indent choice is a matter of taste or house style; two spaces keeps long documents narrow, and four spaces is easier to scan. Minified output is for sending or storing, where size matters and no one reads it.

Find the error: how to read the message

The message starts with the line and column numbers, then explains what was expected. Press ‘Go to the error’ and the cursor jumps to that position in the input box. Look at the character just before the position as well: the parser often notices a problem one step after it happened, for example at the next line when a comma is missing at the end of the previous one.

Messages are written to point at the cause. A message saying that a comma or a closing brace is expected usually means a missing comma between two entries, or a stray character after a value. A message that the JSON ends too early means a bracket, brace or quote was never closed. A message about an unexpected character after the end of the JSON value means there is more text after a complete value, which happens when two JSON documents are pasted together or when a log file holds one JSON object per line.

The mistakes that cause most errors

Most invalid JSON comes from habits borrowed from other languages. JSON allows only double quotes around strings and keys, no comments, no comma after the last item, and no special number values. Each of these has its own message, and the fix is always a small edit.

Strings have rules of their own. A real line break inside a string is not allowed and must be written as the two characters backslash and n. A backslash must be doubled, as in a Windows path. If the error says a control character or an invalid escape was found, look at the string it points to.

  • Trailing comma before ] or }: delete the last comma.
  • Single quotes around a string or a key: replace them with double quotes.
  • Unquoted key such as {name: 1}: write {"name": 1}.
  • Comments with // or /* */: remove them; JSON has no comments.
  • NaN, Infinity or undefined: use null, or a number.
  • A number with a leading zero, a leading plus sign or a bare decimal point such as .5: write 0.5 and drop the plus.
  • True or False with a capital: only lowercase true, false and null are valid.

What the formatter changes and what it keeps

Numbers are written back exactly as you typed them. A 20-digit identifier keeps every digit, and 1.50 stays 1.50, because the tool does not convert numbers into floating-point values. This matters when IDs are longer than a normal program can hold exactly. The order of keys is kept as it is, unless you tick ‘Sort keys A to Z’. Duplicate keys are not merged or flagged; both stay.

Strings are re-written in their plain form. An escape such as \u00e9 is shown as the letter é, and \/ is shown as a slash. The meaning is the same, but if you compare the output against the original text, expect those escapes to differ. A byte order mark at the very start is accepted.

‘Sort keys A to Z’ orders the keys of every object by character code, so capital letters come before lowercase ones: B sorts before a. Arrays are never reordered, because the order of items in an array carries meaning. Sorting is useful before you compare two documents, since the same data in a different key order then looks identical.

Browse large JSON in the tree view

In ‘Tree’, objects and arrays can be opened and closed. The first few levels open on their own for a small document. ‘Expand all’ and ‘Collapse all’ do what they say, and for a very large document only a part may expand, with a note telling you so. Long arrays are shown in pages, with a button such as ‘Show 400 more’ at the end.

Click any row and ‘Path’ shows where it sits, for example $.items[0].name. ‘Copy path’ takes that address, which helps when you must tell someone, or write code, that reaches a given value. Keys with unusual characters appear in bracket form with quotes.

‘Keys’ counts every object key in the document and ‘Depth’ tells you how deeply it is nested. A document nested deeper than the tool can process is reported as too deep.

Limits and good habits

This JSON formatter checks syntax only. It cannot tell you whether a field is named correctly, whether a value is the right type for your program, or whether the data matches a schema. It does not accept comments or trailing commas, and does not convert JSON to or from other formats. Valid JSON can still be wrong data.

Remember what you paste. API responses, tokens and configuration files can contain keys, passwords and personal data. The text is processed in your browser and is not uploaded, but a copy is kept in this browser until you press ‘Clear’, so clear it on a shared computer. Remove secrets before you share formatted output with anyone.

Questions and answers

How do I pretty print JSON?

Paste it into ‘JSON input’ and choose ‘2 spaces’, ‘4 spaces’ or ‘Tab’ under ‘Output’. The formatted version appears in ‘Result’ and can be copied or saved.

How do I find the error in my JSON?

Read the line and column in the message, and press ‘Go to the error’ to jump there. If it looks fine, check the end of the line before it for a missing comma or quote.

Why is JSON with comments or single quotes rejected?

The JSON standard does not allow them, even though some programming languages do. Remove the comments and use double quotes.

Can this tool repair broken JSON?

No. It points to the problem and explains it, but you make the edit yourself.

Does formatting change my numbers or values?

Numbers are kept exactly as typed. Only white space is changed, and string escapes such as \u00e9 are shown as the plain character.

Is my JSON sent to a server?

No. Parsing and formatting happen in your browser. A copy stays in this browser until you press ‘Clear’.