Learn valid JSON syntax with examples
A practical JSON tutorial covering the six value types, proper format rules, common syntax errors, and what pretty-printing, minifying and copy/paste can change.
JSON (JavaScript Object Notation) is a plain-text format for structured data. Its grammar is small: a document holds one value, and that value is built from six kinds of parts. Parsers enforce the rules strictly, so a single stray comma or curly quote makes the whole document invalid. This guide covers the syntax rules, gives a simple JSON example, lists the most common errors, and explains what formatting tools and copy/paste can and cannot change about your data.
A simple JSON example
{
"id": 1024,
"title": "Quick JSON tutorial",
"published": true,
"rating": 4.5,
"editor": null,
"tags": ["json", "syntax", "examples"],
"author": {
"name": "Sam",
"role": "writer"
}
}
This whole document is a single object. Inside it are a number, a string, a boolean, another number with a fractional part, null, an array of strings, and a nested object. Those are all of the building blocks JSON offers. There are no dates, no comments and no functions. Dates are usually stored as strings, for example in ISO 8601 form, and the receiving program interprets them.
The six kinds of value
| Type | Example | Key rule |
| Object | {"key": "value"} | Unordered set of name/value pairs in curly braces |
| Array | [1, "two", false] | Ordered list in square brackets; types may be mixed |
| String | "text" | Double quotes only; some characters must be escaped |
| Number | -12.5e3 | Decimal only; no leading zeros, NaN or Infinity |
| Boolean | true or false | Lowercase literals |
| Null | null | Lowercase literal |
Objects and keys
An object starts with { and ends with }. Each member is a key, then a colon, then a value. Members are separated by commas, and a comma after the last member is not allowed. Keys must be strings in double quotes. Unquoted keys and single-quoted keys are JavaScript habits that JSON rejects. An empty object, {}, is valid.
The JSON standard says keys within one object should be unique, but it does not require it. Parsers handle duplicates differently. Some keep the last value, some keep the first, and some report an error. Avoid duplicates entirely. The standard also treats objects as unordered, so do not write code that depends on keys appearing in a particular sequence.
Arrays
An array is a comma-separated list of values inside [ and ]. Order is meaningful, values can be of any type including other arrays and objects, and an empty array, [], is valid. As with objects, a trailing comma before the closing bracket is an error.
Strings and escape sequences
Strings must use straight double quotes. Inside a string, a double quote or backslash must be escaped with a backslash. Control characters, U+0000 through U+001F, must also be escaped, which means a literal line break inside a string is invalid. You must write \n instead. The permitted escapes are:
- " double quote
- \ backslash
- / forward slash (optional; a plain / is also fine)
- \b backspace
- \f form feed
- \n line feed
- \r carriage return
- \t tab
- \uXXXX a UTF-16 code unit written as exactly four hexadecimal digits
{
"quote": "She said \"hello\"",
"path": "C:\\Users\\docs",
"multiline": "line one\nline two",
"accented": "caf\u00e9",
"emoji": "\ud83d\ude00"
}
Any other character, including accented letters and emoji, can appear directly in a UTF-8 document without escaping. When a character outside the Basic Multilingual Plane is escaped, it takes two \u escapes, a surrogate pair. That is why the grinning-face emoji U+1F600 appears above as \ud83d\ude00. The escaped and unescaped forms represent the same string once parsed.
Numbers
- An optional leading minus sign is allowed; a leading plus sign is not.
- No leading zeros: 7 and 0.7 are valid, 007 is not.
- Digits are required on both sides of a decimal point: .5 and 5. are invalid; write 0.5 and 5 or 5.0.
- An exponent uses e or E with an optional sign, as in 1e3 or 2.5E-4.
- NaN, Infinity, hexadecimal such as 0x1F, and digit separators such as 1_000 are not valid JSON.
The JSON grammar itself sets no limit on number size or precision, but implementations do. Many, including JavaScript, store numbers as IEEE 754 double-precision floats. In those parsers, integers beyond 9007199254740991 (2^53 − 1) can silently lose precision. If you exchange large identifiers, sending them as strings is the safer choice.
Literals, whitespace and the top level
The literals true, false and null must be lowercase. True, FALSE and NULL are errors. Between tokens, only four whitespace characters are allowed: space, tab, line feed and carriage return. The current standard allows any value at the top level, so a bare string or number is a complete JSON document. Some older parsers only accepted an object or array at the top level. JSON has no comment syntax.
Common syntax errors and how to fix them
The following document contains six separate errors:
{
name: 'Sam',
"tags": ["json", "syntax",],
// reviewer note
"count": 007,
"ok": True,
}
- name is an unquoted key; it must be "name".
- 'Sam' uses single quotes; strings need double quotes.
- The array has a trailing comma after "syntax".
- The // line is a comment, which JSON does not support; if the note matters, store it as data.
- 007 has leading zeros; write 7.
- True is capitalized, and the object ends with a trailing comma.
{
"name": "Sam",
"tags": ["json", "syntax"],
"note": "reviewer note",
"count": 7,
"ok": true
}
Many of these mistakes come from treating JSON as a JavaScript object literal. Related formats such as JSON5 and JSONC accept comments or trailing commas. A strict JSON parser will still reject them, so check which format the receiving program expects.
What formatting actually changes
In JSON tools, formatting means layout. Pretty-printing adds indentation and line breaks, and minifying removes them. Both change only the whitespace between tokens. Whitespace inside strings is data and is left alone. A correct formatter therefore produces a document that parses to the same value. The two forms below are equivalent:
{
"name": "Sam",
"tags": ["json", "syntax"],
"count": 7
}
{"name":"Sam","tags":["json","syntax"],"count":7}
Here "format" does not mean fonts, reversed text or mirrored glyphs, which change the stored characters rather than the layout. If letters in a key are replaced with styled Unicode lookalikes, such as mathematical bold letters, the result is a different key and lookups by the original name will fail. Reversing the character order of a JSON document puts the closing brace first, so the document is no longer valid.
Some tools do more than adjust whitespace. They parse the document and write it out again. Depending on the tool, that round trip can:
• sort or reorder keys
• rewrite numbers, for example 1.0 as 1 or 1e2 as 100
• convert escapes such as \u00e9 into the literal character
• drop duplicate keys
• round large integers
The first three keep the parsed value the same but change the text. The last two can change the data itself. These behaviors vary by tool and were not tested for any specific product here. If exact content matters, compare the original and the output with a diff before relying on the result.
Will JSON survive copying and sharing?
JSON often stops parsing after it passes through software built for prose. Typical causes:
• Word processors and some messaging or publishing tools turn straight quotes into curly quotes (“ ”), which JSON does not accept.
• Indentation pasted from rendered web pages can contain non-breaking spaces (U+00A0) or zero-width characters. Outside strings these are not legal whitespace.
• A byte order mark at the start of a file can trip up some parsers. The standard says implementations must not add one when sending JSON, though parsers may choose to ignore it.
• Long snippets can be truncated or wrapped by chat tools.
To reduce these risks, share JSON as a file or inside a code block, edit it in a plain-text or code editor, and run it through a validator after pasting.
python -m json.tool data.json
jq . data.json
Either command checks a file locally. On success it prints the document pretty-printed, and on failure it reports where the error is. Before pasting data into an online validator, consider what the data contains. A tool's appearance or marketing says nothing reliable about whether it stores what you submit, so keep credentials, personal data and other sensitive content in local tools.
For human readers, indented JSON is easier to scan and to review in diffs, because each key sits on its own line. Minified JSON is compact for transport but hard to read. No documentation or test results were available for this article on how particular screen readers or other assistive technologies present JSON. Do not assume a punctuation-dense code block is accessible. If the audience is people rather than programs, include a plain-language summary or a table of the important fields alongside the JSON.
Should you reformat, and when is plain text better?
For most uses, reformatting is safe and useful. Pretty-print JSON when you need to read, edit or review it, and minify it when size matters for storage or transfer. The main benefit is that the data stays the same while readability changes. The decisive tradeoff is that some tools re-serialize the document and may alter key order, number representation, duplicates or large numbers. Check any tool you depend on against a sample of your own data.
Leave the text untouched when exact bytes matter. Signed payloads, checksums, hashes and test fixtures compared byte for byte will all break after any reformatting, even whitespace-only changes. When the reader is a person rather than a parser, a prose explanation or a table is usually clearer than raw JSON. When the destination is known to rewrite quotes or spacing, send the JSON as an attachment instead of pasted text.
Evidence gaps
The syntax rules above follow the published JSON standard and general, verifiable parser behavior. No tool-specific test results, no assistive-technology testing and no destination-specific copy/paste tests were available. Statements about how any particular formatter, platform or screen reader behaves should be confirmed against that product's documentation or with your own scoped test.