JSON Validator

Check whether a document is valid JSON, see the first error with its line and column, and format a valid document without changing a single value.

Overview

JSON (RFC 8259) has a small, strict grammar, and the usual reason a document is not valid is a detail that reads well to a person: a comma before a closing brace, a key in single quotes, a value that JavaScript accepts and JSON does not. The validator finds the first place the grammar breaks and says what is wrong there, in the same words in every browser.

Web interface

Paste a document into the input; it is checked on every keystroke. The verdict under the input either says Valid JSON, with the size, the number of keys, objects and arrays and the depth, or names the error and quotes its line with a caret under the column. As long as the document does not parse, nothing else on the page describes it.

The formatted output takes an indent of 2, 4 or Tab, or Minified, and three switches: Sort keys, Escape unicode and Compact arrays. Structure shows the tree, the count of each type and the key names that recur. The dice in the corner of the input loads a random example, valid or with a typical mistake.

Errors

Only the first error is reported. After it, JSON gives a parser no reliable way to carry on, so a second message would be a guess. Lines and columns are counted from 1, in characters as a reader counts them, so an emoji is one.

MistakeMessage
[1, 2, ]A trailing comma is not allowed in JSON
{'a': 1}, {a: 1}Property names must be wrapped in double quotes, with not single quotes where that is the case
[1, 2}This array is closed with a brace, it needs a bracket
undefined, NaN, Trueundefined is not a JSON value, use null, JSON has no way to write NaN or Infinity, True must be written in lower case
012, 1., 1eA number may not have a leading zero, Expected a digit after the decimal point, Expected a digit in the exponent
"\x", "\u12", a tab inside a string\x is not a valid escape sequence, \u must be followed by four hexadecimal digits, A raw control character is not allowed inside a string, escape it instead
{"a": 1 /* note */}Expected a comma or a closing brace: JSON has no comments
{} xUnexpected content after the end of the JSON value
an invisible byte order mark at the startThe document starts with an invisible byte order mark (U+FEFF); JSON does not allow one, remove it

The scanner that finds these is held by a test to agree with JSON.parse about what is valid, across sixty thousand generated documents, and to point at the right line and column.

Formatted output

The output is the same document with other whitespace. Numbers keep the text they are written in: 12345678901234567890 keeps every digit, where a JavaScript number would make it 12345678901234567000, and 19.90 stays 19.90. Strings, escapes and the order of keys stay as they are unless a switch says otherwise:

SwitchEffect
Sort keysorders the keys of every object; JSON itself defines no order
Escape unicodewrites every character beyond ASCII as \uXXXX, an emoji as its two halves, as JSON requires
Compact arrayskeeps an array of plain values on one line when that line is shorter than 100 characters

A key that appears twice in one object is not an error in JSON; the last value counts. The output can only hold one of them, so the verdict names the key: The key "a" appears twice in one object; only the last value is kept.

Limits

The validator reads documents nested up to 512 levels. JSON itself sets no limit, so a deeper document is reported as Nested more than 512 levels deep, which this tool does not read rather than as invalid JSON.

A document of 4.4 MB was checked and formatted in about 0.3 seconds in Chrome. The check runs on every keystroke, so very large documents make typing in the input slow; paste them instead.

No API

There is no API for this tool. Validation runs in the browser; in a script, the JSON parser of the language at hand answers the same question, and jq . file.json does on the command line.

Privacy

Nothing leaves the browser. The document is parsed on the page, nothing is stored, and the tool works with the network disconnected.