About JSON Formatter & Validator
Paste JSON and the tool parses it with your browser's own JSON engine. Valid input gets a confirmation note and comes back indented with 2 spaces, 4 spaces or tabs, or minified onto one line, with an option to sort every object's keys alphabetically. A Structure panel counts keys and arrays and reports the maximum nesting depth, the output size and how many characters minifying saves. Invalid input returns the parser's error message, which in Chrome and Edge gives, for most errors, the position, line and column where parsing failed. Because the document is re-serialised from parsed data, a few things are normalised: 1.0 becomes 1, a duplicate key keeps only its last value, and integers beyond 9,007,199,254,740,991 can lose precision.
JSON parse error messages and how to fix them
Messages are shown as Chrome and Edge word them, each produced by running the broken input through this tool. Firefox and Safari report the same problems in different words.
| Broken input | Error message | Cause and fix |
|---|---|---|
| {"a":1,} | Expected double-quoted property name in JSON at position 7 (line 1 column 8) | Trailing comma in an object. Delete the comma before the closing brace. |
| [1,2,3,] | Unexpected token ']', "[1,2,3,]" is not valid JSON | Trailing comma in an array. Delete the last comma. |
| {'a':1} or {a:1} | Expected property name or '}' in JSON at position 1 (line 1 column 2) | Single-quoted or unquoted key. Keys must be in double quotes. |
| {"a":1 "b":2} | Expected ',' or '}' after property value in JSON at position 7 (line 1 column 8) | Missing comma between two properties. The same message appears for a // comment, a semicolon used as a separator, or a missing closing brace. |
| ["a","b" | Expected ',' or ']' after array element in JSON at position 8 (line 1 column 9) | The array is never closed, or a comma is missing between items. |
| {"a"=1} | Expected ':' after property name in JSON at position 4 (line 1 column 5) | An equals sign or nothing where the colon should be. |
| {"a":undefined} | Unexpected token 'u', "{"a":undefined}" is not valid JSON | undefined is not a JSON value. Use null or remove the key. |
| {"a":NaN} or {"a":None} | Unexpected token 'N', ... is not valid JSON | NaN, Infinity and Python's None do not exist in JSON. Use null. |
| {"a":True} | Unexpected token 'T', "{"a":True}" is not valid JSON | Literals are lowercase only: true, false and null. |
| {"a":01} | Unexpected number in JSON at position 6 (line 1 column 7) | Leading zeros are not allowed. Write 1, or quote the value if it is a code such as "01". |
| {"a":.5} or {"a":+1} | Unexpected token '.', ... or Unexpected token '+', ... | A number needs a digit before the decimal point and cannot start with a plus sign. Write 0.5 and 1. |
| A string containing a real line break or tab | Bad control character in string literal in JSON at position ... | Write line breaks as \n and tabs as \t inside strings. |
| {"a":"unterminated} | Unterminated string in JSON at position 19 (line 1 column 20) | A closing double quote is missing. |
| A string containing \x | Bad escaped character in JSON at position ... | Only \" \\ \/ \b \f \n \r \t and \u followed by four hex digits are valid escapes. |
| {"a":1}{"b":2} | Unexpected non-whitespace character after JSON at position 7 (line 1 column 8) | Two documents in one paste, as in JSON Lines logs. Format one line at a time or wrap them in an array. |
| <html><body>502</body></html> | Unexpected token '<', "<html><bod"... is not valid JSON | The response is an HTML page, not JSON. Check the URL and the status code of the request. |
Position counts characters from 0; line and column count from 1. The parser stops at the first problem, so fix it and look again for the next.
Formatting, minifying and sorting examples
Results and Structure figures are taken from the tool.
Beautify the default sample
{"name":"Ada","languages":["JS","TS"],"active":true} with Beautify, 2 spaces
8 lines, 78 characters. Structure: Keys 3, Arrays 1, Max depth 3. With 4 spaces the same document is 94 characters.
Minify a nested API response
An object with a users array of two objects and a meta object, indented with 4 spaces (266 characters), Output set to Minify
{"users":[{"id":1,"tags":["a","b"]},{"id":2,"tags":[]}],"meta":{"page":1,"total":2}} Size 84 chars, Saved by minifying 182 chars. Keys 8, Arrays 3, Max depth 5.
The same document is 188 characters with 2-space indentation and 149 with tabs.
Sort keys at every level
{"zip":"10001","city":"New York","address":{"street":"5th Ave","number":350}} with Sort keys alphabetically ticked
Top-level order becomes address, city, zip, and inside address the order becomes number, street.
Arrays keep their original order. Only object keys are sorted.
A 64-bit identifier
{"id":9007199254740993}
{ "id": 9007199254740992 }
The last digit changed. Integers above 9,007,199,254,740,991 are not all representable as a JavaScript number, so send large IDs as strings.
Number spellings and a duplicate key
{"price":1.0,"qty":1e3,"rate":0.10} and, separately, {"a":1,"a":2}, both minified
{"price":1,"qty":1000,"rate":0.1} and {"a":2}
JSON syntax rules compared with a JavaScript object literal
Most invalid JSON is valid JavaScript. These are the differences that matter.
| Feature | JSON | JavaScript object literal |
|---|---|---|
| Key quoting | Double quotes required | Optional for simple names |
| String quotes | Double quotes only | Single, double or backticks |
| Trailing comma | Not allowed | Allowed |
| Comments | Not allowed | Allowed |
| undefined, NaN, Infinity | Not allowed | Allowed |
| true, false, null | Lowercase only | Lowercase only |
| Leading zero or plus sign on numbers | Not allowed | Plus sign allowed |
| Hexadecimal numbers such as 0x1F | Not allowed | Allowed |
| Top-level value | Any value: object, array, string, number, true, false or null | Not applicable |
Files such as tsconfig.json and VS Code settings use JSON with Comments, a separate dialect. A strict parser, including this one, rejects their comments and trailing commas.
What the Structure panel counts
- Keys is the total number of object properties at every level. The users and meta example has 8: users, meta, two id, two tags, page and total.
- Arrays is the number of arrays anywhere in the document, including empty ones.
- Max depth counts the root value as level 1 and adds one for each nested object or array. A flat object of plain values has depth 2.
- Size is the length in characters of the output currently shown, so it changes with the indent you pick.
- Saved by minifying is the length of what you pasted minus the length of the minified form. It is 0 when the input is already on one line with no spaces.
How Sort keys alphabetically orders keys
Sorting is applied to every object in the document, however deeply nested, and compares keys the way your browser's locale-aware text comparison does. Lowercase comes before uppercase for the same letter, so b is placed before B, and a key starting with an underscore comes before letters.
Keys that are whole numbers behave differently, and this is true whether or not you tick the box. JavaScript always lists integer-like keys first, in ascending numeric order, so {"b":1,"2":5,"a":3,"10":4} is output as 2, 10, b, a even with sorting off. With sorting on it becomes 2, 10, a, b.
Reordering keys does not change the meaning of a JSON document, because the specification treats an object as an unordered collection. That makes sorting a safe way to normalise two documents before comparing them: with the same indent and sorted keys, a text diff shows only the values that differ.
How to get your result
- 1
Paste your document into the JSON box, replacing the sample object.
- 2
Choose an Output: Beautify with 2 spaces, 4 spaces or tabs, or Minify.
- 3
Tick Sort keys alphabetically if you want every object's keys in a fixed order, for example before comparing two documents.
- 4
If the input is valid, a Valid JSON note appears. Copy the Formatted JSON or Minified JSON, or download it as data.json.
- 5
If you see Invalid JSON, read the message for the reason and, where given, the position, line and column, then fix that spot.
- 6
Check the Structure panel for Keys, Arrays, Max depth, Size and Saved by minifying.