JSON Syntax Explained: Grammar, Types & RFC 8259 Rules
Deep dive into RFC 8259 JSON syntax rules. Learn precise escaping sequences, string encoding, number formatting, and bracket balancing to write valid JSON.
The RFC 8259 Grammar Specification
The Internet Engineering Task Force (IETF) defines the JSON standard in RFC 8259. Unlike flexible scripting languages like JavaScript, JSON is a strict data representation format with deterministic grammar rules.
String Escaping Rules
Strings in JSON must be wrapped in double quotes and encoded in UTF-8. Certain characters must be escaped with a preceding backslash (\):
| Character | Escape Sequence | Meaning |
|---|---|---|
" | \" | Quotation mark |
\ | \\ | Reverse solidus (Backslash) |
/ | \/ | Solidus (Forward slash - optional) |
Backspace | \b | Backspace control code |
Form feed | \f | Form feed control code |
Newline | \n | Line feed (LF) |
Carriage return | \r | Carriage return (CR) |
Tab | \t | Horizontal tab |
| Unicode | \uXXXX | 4-digit hexadecimal Unicode point |
Number Representation Rules
JSON numbers adhere to standard mathematical notations with strict constraints:
{
"integer": 42,
"negative": -100,
"fraction": 3.14159265,
"scientific_positive": 1.25e6,
"scientific_negative": 4.8e-3
}
Disallowed: NaN, Infinity, -Infinity, hexadecimal numbers (0xFF), and octal numbers (077) are invalid in JSON.
Objects vs. Dictionaries
A JSON object represents a key-value map where each key is a unique string. While the specification allows duplicate keys, RFC 8259 warns that behavior is unpredictable across parsers. Always ensure object keys are distinct:
// Correct:
{
"hostname": "api.toolnest.dev",
"port": 443,
"tlsEnabled": true
}