What JSON is, its syntax rules, and how to read a JSON error
JSON's six value types and strict rules in plain words, why parsers reject trailing commas and single quotes, and how to find a mistake from its line and column.
Open the JSON formatter Free, in your browser, nothing uploaded.
JSON is the text format most apps, websites and settings files use to pass data around. It is simple, but strict: one stray comma and a whole file is rejected, often with an error that names a position rather than a problem. This article explains what JSON is, the rules a valid file must follow, and how to read the mistakes the JSON formatter points out. It is for developers, testers, analysts who get data from an API, and students meeting JSON for the first time. When two versions of a file disagree, JSON compare shows what changed; to turn JSON into YAML, XML, TOML or CSV, use config converter; and to pull values out of log lines, try regex tester.
What it is
JSON stands for JavaScript Object Notation. It grew out of JavaScript but is now its own standard (RFC 8259 and ECMA-404), read by almost every programming language.
A JSON document is one value, and there are exactly six kinds:
- Object: keys and values in curly braces,
{"city": "Pune"}. Keys are always strings in double quotes. - Array: an ordered list in square brackets,
[2, 1]. - String: text in double quotes, with escapes such as
\",\\,\nandA. - Number:
42,-3.5,1e6. No leading zeros, no+sign, noNaNorInfinity. - true and false.
- null.
Objects and arrays can nest to any depth.
What JSON does not allow surprises people who know JavaScript: single quotes, comments, a comma after the last item, keys without quotes, and text after the closing brace. A parser that follows the standard stops at the first of these.
Spaces, tabs and line breaks between values carry no meaning. That is why the same data can be minified to one line for sending, or pretty-printed with indents for reading, and both are equally valid.
How to use it
- Paste the JSON into Paste your JSON, or press Example to load a sample.
- Choose Pretty-print or Minify. For text that has to sit inside another string, use Escape as string, or Unescape string to turn it back.
- Pick the Indent (2 spaces, 4 spaces or a tab) and tick Sort keys A to Z if you want them in order.
- If there is a mistake, read the message with its line and column, press Go to it, and fix the spot shown with ”^ here”.
- Browse nested data in the tree with Open all and Fold all.
- Press Copy result or Download, or Clear to start again.
A worked example
An online shop’s order arrives from an API as one line:
{"order":1042,"items":[{"sku":"A7","qty":2},{"sku":"B3","qty":1}],"paid":true,"customer":{"name":"Asha","city":"Pune"}}
That is 119 characters. Pretty-printed with 2 spaces, it becomes 18 lines and 206 characters: each key on its own line, each item in the list in its own block. With 4 spaces it grows to 266 characters. The data is identical in all three; only the white space changes.
Sorting the keys puts customer first, then items, order and paid, and inside each item qty comes before sku. The list keeps its order: item A7 is still first, because order in an array means something.
Now a broken copy. Someone edited it by hand and lost a comma:
{"order": 1042,
"items": [{"sku": "A7", "qty": 2}, {"sku": "B3" "qty": 1}],
"paid": true}
The formatter reports Mistake at line 2, column 50: Expected ”,” or ”}” but found """. Column 50 is the opening quote of the second "qty". The parser had just read the value "B3", so the only things allowed next were a comma (more keys follow) or a closing brace (the object ends). It found another quote instead. Add the comma after "B3" and the file is valid.
A last test: {"order": 1042, "note": 'gift'} gives line 1, column 25: Strings need double quotes (”), not single quotes.
Checked with the tools
Each number above came from the formatter’s own parser (src/lib/dev/json.ts) run in Node. tryParse() returned the two errors with the same line, column and message the page shows; sortKeys() gave the key order; and the pretty-printed lengths come from the same JSON.stringify call the tool makes, at indents of 2 and 4. The tool counts bytes, which equal characters here because the example is plain ASCII.
Where it is used
- Web and app development. API replies, config files such as
package.json, and browser storage are all JSON. - Testing and support. Testers paste a reply into a formatter to see whether a field is missing or
null. - Data analysis. Exports from survey tools, analytics and open-data portals often come as JSON before they become a table.
Common mistakes
- A comma after the last item. JavaScript allows it, JSON does not. Delete it.
- Single quotes or unquoted keys. Use double quotes around every key and every string.
- Comments.
//and/* */are not JSON. Move notes into a"_note"field or a separate file. - Long IDs as numbers. Most parsers store numbers as doubles, so
9007199254740993comes back as9007199254740992. Keep big IDs as strings. - Line breaks inside a string. A string cannot run onto a new line; write
\ninstead.
Questions people ask
Is JSON the same as a JavaScript object? No. JSON is text with stricter rules. Every valid JSON document is valid JavaScript, but not the other way round.
Does key order matter? Not to the meaning of an object. It can matter to people comparing two files, which is why sorting helps.
What is JSON Lines? A file with one complete JSON value per line, used for logs and large exports. Each line must be valid on its own; the whole file is not one JSON document.
Sources
- JSON syntax as defined in RFC 8259 and ECMA-404, checked by fibub's own small parser. fibub's own working.
If you are an author or publisher and want a credit or a passage changed or removed, tell us and we will act quickly.