Last checked by the TextEditor.com team
What JSON is
JSON is a text format for data. It looks like the way JavaScript writes objects, which is where the name comes from. Douglas Crockford described the rules in 2001 and put them on json.org. Today two standards define it, RFC 8259 from 2017 and ECMA-404, and they agree with each other.
The whole grammar fits on one page. A JSON text is one value. A value is an object, an array, a string, a number, true, false or null. Objects hold key and value pairs, arrays hold values in order. That is all, and it is why every language can read it.
The grammar in one figure
{
"name": "Mia",
"age": 34,
"member": true,
"nickname": null,
"scores": [9.5, 8, 10],
"address": { "city": "Oslo", "zip": "0150" }
}
- Object
{ }with pairs of"key": value, separated by commas. Keys are strings. The order does not matter.- Array
[ ]with values separated by commas. The order matters. Values may be of different types.- Key
- A string in double quotes, followed by a colon. Any text works, even with spaces.
- String
- Text in double quotes. Special characters use a backslash:
\"\\\n\té. Single quotes are not allowed. - Number
- Decimal digits, an optional minus sign, a decimal part and an exponent:
-12,3.14,1e10. No leading zeros, no plus sign, no hex, noNaNorInfinity. - Boolean
trueorfalse, in lowercase, without quotes.- Null
nullmeans "no value". NotNone, notnil, notundefined.
The five most common errors
Almost every "invalid JSON" message comes from one of five mistakes, and four of them are things that JavaScript allows but JSON does not. The validator above names the mistake and the line. The Repair button fixes all five and tells you what it changed.
| Mistake | Wrong | Right |
|---|---|---|
| Trailing comma | {"a": 1,} | {"a": 1} |
| Single quotes | {'a': 'x'} | {"a": "x"} |
| Unquoted key | {a: 1} | {"a": 1} |
| Comment | {"a": 1 // note} | {"a": 1} |
| Missing comma | {"a": 1 "b": 2} | {"a": 1, "b": 2} |
Trailing comma: there is a comma before this }. Remove the last comma.
JSON or a JavaScript object literal?
They look alike, and that is the trap. A JavaScript object can be written in many ways. JSON allows exactly one. Since 2019 every valid JSON text is also valid JavaScript, but the reverse is far from true.
| Feature | JSON | JavaScript object |
|---|---|---|
| Keys | Always in double quotes | Bare names or any quotes |
| Strings | Double quotes only | Single, double or backticks |
| Trailing comma | Not allowed | Allowed |
| Comments | Not allowed | Allowed |
| Values | Object, array, string, number, true, false, null | Also undefined, functions, dates, NaN, regular expressions |
| Numbers | Decimal only | Also hex (0xFF), octal, binary, BigInt (12n) |
The Convert menu above turns JSON into a JavaScript object literal with bare keys, which is what you paste into a .js file. The other way, use Repair: it quotes the keys and swaps the quotes.
Comments: JSON5 and JSONC
JSON has no comments, by design. Crockford removed them because people used them to carry parsing directions. Two relaxed versions brought them back. JSONC is JSON plus // and /* */ comments, and often trailing commas. VS Code uses it for settings.json, and TypeScript accepts it in tsconfig.json. JSON5, from 2012, goes further: single quotes, unquoted keys, hex numbers, Infinity and multi line strings.
Outside those tools, assume strict JSON. npm fails on a comment in package.json, and web APIs reject it. If a file with comments must become real JSON, paste it above and click Repair. It removes the comments and lists how many.
Numbers and precision
The JSON grammar puts no limit on a number. The programs that read it do. JavaScript stores every number as a 64 bit float, which holds whole numbers exactly only up to 253 minus 1, that is 9,007,199,254,740,991. One digit more and the last digits get rounded. Twitter ran into this with tweet IDs in 2010 and added an id_str field as a string. Python and Java keep big integers exact.
Most online formatters run on JavaScript and quietly change such numbers when they format. This page keeps every number exactly as you wrote it, because it never turns it into a JavaScript number. Type a long number below to see what JavaScript would make of it.
JavaScript reads it as 9007199254740992. That is not the same number. Send it as a string: "9007199254740993".
Where JSON is used
Web APIs answer in JSON, and nearly every programming language has a JSON reader built in. Configuration files use it: package.json for npm, tsconfig.json, composer.json, the settings of VS Code. Browsers keep data in it through localStorage. Databases like MongoDB and CouchDB store documents as JSON or a binary twin of it. Log systems write one JSON object per line, a form called NDJSON. Jupyter notebooks are JSON files.
When the data is a list of records, a table is easier to read. Convert it to CSV above and open it in the CSV editor or the table editor. For HTML, CSS or Python files use the code editor, which this page is built on.
More tools for data
- Edit a CSV file
a,b,cThe CSV editor puts each value in its own cell. - Edit a spreadsheet
.xlsx▦The table editor opens Excel files and cells you paste. - Encode or decode textHi → SGk=Base64, URL codes, HTML entities and six more formats.
- Edit code
{ }The code editor colors your code and saves as you type.