Skip to content
thecalculatorstudio

JSON Formatter and Validator

Paste JSON to check it against RFC 8259 and format it with 2 or 4 spaces or a tab, or minify it. Errors show the line and column. It all happens in your browser.

Last reviewed

Everything happens in your browser. The JSON is not sent anywhere or saved. Numbers and strings are kept exactly as you wrote them.

Valid JSON

{
  "name": "Ada",
  "age": 36,
  "languages": [
    "English",
    "French"
  ],
  "address": {
    "city": "London",
    "postcode": null
  },
  "active": true,
  "score": 12.50
}
Objects
2
Arrays
1
Keys
8
Deepest nesting
3
Size before
128 bytes
Size after
181 bytes

What JSON is

JSON (JavaScript Object Notation) is a plain-text format for data, defined in RFC 8259. It has two structures: an object, a set of name and value pairs in curly braces, and an array, an ordered list of values in square brackets. A value is a string, a number, true, false, null, an object or an array.

The rules the validator checks

From RFC 8259:

  • The six structural characters are [ ] { } : and ,.
  • Whitespace is only a space, a tab, a line feed or a carriage return.
  • The literal names are true, false and null, in lower case.
  • A number has an optional minus sign, an integer part without leading zeros, an optional fraction and an optional exponent. Infinity and NaN are not allowed.
  • A string is in double quotes. Quotation marks, backslashes and control characters must be escaped, using " \ / \b \f \n \r \t or \uXXXX.
  • The name of an object member must be a string, and a value must follow every comma, so a trailing comma is an error.

What the tool does

  • Validate. It reads the text with its own parser, which follows the grammar exactly, and stops at the first error with the line and column.
  • Format. It re-indents the structure with 2 spaces, 4 spaces or a tab.
  • Minify. It removes all the whitespace that is not inside a string.
  • Sort keys. Optionally, it orders the keys of every object A to Z.
  • Count. It reports the objects, arrays, keys, depth and sizes.

Numbers and strings are copied exactly as you wrote them. Many languages parse a number into a double and write it back, which turns 12345678901234567890 into 12345678901234567000 and 1.0 into 1. This tool does not.

Common errors and what the tool says

JSON Message
{"a":1,} Expected a string key in double quotes but found “}” (line 1, column 8)
{'a':1} Expected a string key in double quotes but found “’” (line 1, column 2)
{"a":01} A number cannot have a leading zero (line 1, column 7)
{"a" 1} Expected “:” after the key but found “1” (line 1, column 6)
[1 2] Expected “,” or “]” but found “2” (line 1, column 4)
{"a":1 Expected “,” or “}” but found the end of the text (line 1, column 7)

The first row is the trailing comma, the second the single quotes of a JavaScript object, and the third a number with a leading zero. A raw line break inside a string is also an error, which must be written \n.

Tips

  • Format first when you debug. An error is easier to find in an indented document.
  • Look just before the position. The reported column is where the parser noticed the problem, which is often after the cause, such as a missing comma on the line above.
  • Use minified JSON for transport and the indented form for reading.
  • Do not paste secrets you would not share. The text stays in your browser and is not sent, but treat tokens and keys with care.

Common mistakes

  • Using single quotes.
  • Leaving a trailing comma after the last item.
  • Adding comments. JSON has none.
  • Writing keys without quotes.
  • Writing a number as .5 or 5. It must be 0.5 and 5.0 or 5.

To read the parts of a JSON Web Token, use the JWT decoder. For binary data in JSON use the base64 encoder and decoder, and for a pattern search the regex tester.

How it is calculated

The text is parsed by a recursive-descent parser written for this page, which allows up to 200 levels of nesting and 2,000,000 characters. The formatter writes the tokens back as they were read, so nothing is rounded.

Frequently asked questions

What makes JSON invalid?

The usual causes are a trailing comma, single quotes instead of double quotes, keys without quotes, a missing comma or colon, comments, and numbers like 01, .5 or +1. RFC 8259 allows none of these. The tool points to the first problem by line and column.

Are single quotes allowed in JSON?

No. Strings and keys must use double quotes. A JavaScript object such as {'a': 1} is not JSON. The tool reports that it expected a string key in double quotes.

Is a trailing comma allowed?

No. The grammar requires a value after every comma, so [1, 2, ] and {"a": 1,} are errors. Programs that accept them are using an extension of JSON.

Does formatting change my numbers?

No. This tool keeps every number and string exactly as you wrote it, so 12345678901234567890 and 12.50 are not rounded or rewritten. A parse and re-serialise in many languages would turn the first into 12345678901234567000.

What does sorting the keys do?

It orders the keys of every object A to Z, which makes two files easier to compare. The order of items in an array is never changed, because it can carry meaning.

What are duplicate keys?

RFC 8259 says the names within an object should be unique. When one repeats, different programs keep the first, the last or fail. The tool formats the text as written and lists the repeated keys so you can fix them.

Related calculators

Sources