Tham chiếu
Tham chiếu cú pháp JSON
Tham chiếu đầy đủ cú pháp JSON (RFC 8259) với loại dữ liệu, quy tắc ngữ pháp, ví dụ và lỗi phổ biến.
Last updated: 2026-08-28
| category | name | syntax | description |
|---|---|---|---|
| Type | String | "hello world" | Unicode character sequence wrapped in double quotes. Supports escape sequences: \" \\ \/ \b \f \n \r \t \uXXXX |
| Type | Number | 42, -3.14, 1e10, 2.5E-3 | IEEE 754 double-precision float. No leading zeros (except 0 itself), no NaN, no Infinity, no hexadecimal |
| Type | Boolean | true | false | Literal lowercase keywords true or false (not quoted, not capitalized) |
| Type | Null | null | Literal lowercase keyword null representing absence of value (not quoted) |
| Type | Object | {"key": value, ...} | Unordered collection of key-value pairs. Keys must be unique strings. Empty object: {} |
| Type | Array | [value, value, ...] | Ordered sequence of values of any type. Empty array: []. Values can be mixed types |
| Rule | Double quotes only | "key": "value" | Strings and keys must use double quotes. Single quotes are invalid |
| Rule | Quoted keys | {"name": "Alice"} | Object keys must be wrapped in double quotes (unlike JavaScript object literals) |
| Rule | No trailing comma | {"a": 1, "b": 2} | The last element in an object or array must not have a trailing comma |
| Rule | Root structure | {...} or [...] | Top-level value must be an object or array (per RFC 8259, any value is valid, but many parsers require object/array) |
| Rule | No comments | N/A | JSON does not support // or /* */ comments (unlike JSON5 or JavaScript) |
| Rule | Escape sequences | "line1\nline2" | Use backslash to escape: \" \\ \/ \b \f \n \r \t \uXXXX (4 hex digits) |
| Rule | Unique keys | {"a": 1, "b": 2} | Object keys should be unique. Duplicate keys produce undefined behavior (last wins in most parsers) |
| Rule | Number format | -?[0-9]+(.[0-9]+)?([eE][+-]?[0-9]+)? | Optional minus, integer part, optional fraction, optional exponent. No leading zeros, no + sign, no hex |
| Example | Simple object | {"name": "Alice", "age": 30, "active": true} | A person object with string, number, and boolean values |
| Example | Nested structure | {"user": {"id": 1}, "tags": ["a", "b"]} | Object containing nested object and array |
| Example | Array of objects | [{"id": 1}, {"id": 2}] | Array containing multiple objects (common in API responses) |
| Example | Mixed array | [1, "two", true, null, [5]] | Array with mixed types: number, string, boolean, null, nested array |
| Example | Empty values | {}, [], "" | Empty object, empty array, and empty string are all valid JSON values |
| Error | Single quotes | {'key': 'value'} | INVALID: JSON requires double quotes for strings and keys |
| Error | Trailing comma | {"a": 1, "b": 2,} | INVALID: Trailing comma after the last element is not allowed |
| Error | Unquoted keys | {name: "Alice"} | INVALID: Object keys must be wrapped in double quotes |
| Error | Comments | {/* comment */ "a": 1} | INVALID: JSON does not support comments of any style |
| Error | NaN / Infinity | {"value": NaN} | INVALID: NaN, Infinity, and -Infinity are not valid JSON numbers |
| Error | Leading zeros | {"value": 007} | INVALID: Numbers cannot have leading zeros (use 7, not 007) |
| Error | Undefined | {"value": undefined} | INVALID: undefined is not a JSON value (use null for absence) |
| Error | Hex numbers | {"value": 0xFF} | INVALID: Hexadecimal literals are not supported (use decimal 255) |
| Error | Missing quotes | {name: Alice} | INVALID: Both keys and string values require double quotes |