The rules JSON actually enforces
JSON allows six kinds of value: objects in curly braces, arrays in square brackets, strings in double quotes, numbers, true and false, and null. Keys must be strings in double quotes. Values are separated by commas, and there must be no comma after the last item.
Anything else, including comments, single quotes, unquoted keys, undefined, NaN, Infinity and trailing commas, is invalid JSON even though many of these work in JavaScript source code. That difference is the source of most confusion.
Ten common mistakes, with the real error messages
The messages below are from the JSON parser in Node.js. Other parsers word them differently, but the cause is the same.
| Invalid JSON | What is wrong | Typical error |
|---|---|---|
| {'a': 1} | Single quotes around the key | Expected property name or "}" in JSON at position 1 |
| {"a": 1,} | Trailing comma in an object | Expected double-quoted property name at position 7 |
| [1, 2, 3,] | Trailing comma in an array | Unexpected token "]" |
| {a: 1} | Unquoted key | Expected property name or "}" at position 1 |
| {"a": 1 "b": 2} | Missing comma between pairs | Expected "," or "}" after property value at position 7 |
| {"a": undefined} | undefined is not a JSON value | Unexpected token "u" |
| {"a": NaN} | NaN and Infinity are not allowed | Unexpected token "N" |
| {"a": 01} | Leading zero in a number | Unexpected number at position 6 |
| // note then {"a": 1} | Comments are not allowed | Unexpected token "/" |
| a string with a real line break inside quotes | Control characters must be escaped as \n | Bad control character in string literal |
How to find the error fast
- Read the position. Most parsers report a character offset or a line and column. Look just before it: the real mistake is often the previous character, such as a missing comma at the end of the line above.
- Paste the document into a JSON formatter. It highlights the first invalid spot and, once valid, pretty-prints the structure so you can see nesting problems.
- Check brackets in pairs. Every opening brace or bracket must be closed in the reverse order. Collapsing sections in an editor helps.
- Look at quotes. Curly "smart quotes" copied from a document or chat are not valid JSON quotes, even though they look similar.
- Check for hidden characters, such as a byte-order mark at the start of a file or a non-breaking space.
Things that do not survive a round trip
Converting a JavaScript object to JSON drops or changes some values. With JSON.stringify, undefined values and functions are omitted from objects and become null inside arrays, NaN and Infinity become null, and a Date becomes an ISO text string such as "1970-01-01T00:00:00.000Z".
When you read that data back you get strings, not Date objects, so convert them yourself. If a value must survive exactly, store it in a format that supports it, or encode it as a string on purpose.
Tips to avoid errors
- Generate JSON with a library (JSON.stringify, json.dumps) instead of building strings by hand.
- Validate incoming JSON in your application and handle parse failures with try/catch rather than crashing.
- If you need comments or trailing commas for configuration files, use a format designed for it, such as JSON5 or YAML, and convert to strict JSON when needed.
- Format and minify with a tool. Pretty-printed JSON is easier to read; minified JSON is smaller to send.
Frequently asked questions
Why does my JSON work in JavaScript but not in a parser?
JavaScript object literals allow single quotes, unquoted keys, trailing commas and comments. JSON does not. A snippet that runs as code can still be invalid JSON.
Can JSON have comments?
No. The standard has no comment syntax. Some tools accept them as an extension, but a strict parser will reject them.
What is the difference between JSON and a JavaScript object?
JSON is a text format for exchanging data. A JavaScript object is an in-memory value. JSON.parse turns text into an object and JSON.stringify turns an object into text.
How large can a JSON file be?
The format has no limit, but parsers load it into memory. Very large files are better handled with a streaming parser or a line-based format.
Try these tools
JSON Formatter
Format, validate, beautify and minify JSON instantly. Shows the exact line and column of any error. Free and runs in your browser.
Open toolJSON Diff
Compare two JSON documents and see added, removed and changed paths in colour. Key order is ignored, so only real differences are reported. Free and private.
Open toolJSON to CSV
Convert a JSON array to CSV in your browser. Flattens nested objects, picks delimiters and downloads a ready .csv for Excel or Sheets.
Open toolJSON to YAML Converter
Convert JSON to clean, readable YAML instantly. Choose indent size, copy or download the result. Free and runs in your browser.
Open toolJSONPath Tester
Test JSONPath expressions against your JSON. See every match with its path. Supports $, ., [], [*], .., slices and simple filters. Free and private.
Open toolKeep reading
JSON vs CSV vs YAML vs XML: when to use each, and what breaks when you convert
Compare JSON, CSV, YAML and XML with examples, learn when each fits, and avoid conversion pitfalls like nested data, lost types, leading zeros and BOMs.
Read guideFree online tools with no sign-up and no upload: what to look for
How to judge whether a free online tool site is trustworthy: signs of hidden uploads, account walls and ad pressure, plus how our own approach compares.
Read guideHow to convert JSON to CSV, with a worked example
Turn a JSON array into a CSV you can open in Excel or Google Sheets. See how nested objects and arrays are flattened, with a worked example and common pitfalls.
Read guide