Introduction to JSON Syntax
This in-depth tutorial explores the syntax of JSON, its rules, structure, and formatting standards required for writing valid JSON data. You will learn key syntax principles through clear examples and gain the skills to avoid common parsing issues like parse_error 101. By the end, you will be equipped to write proper JSON objects, arrays, and key-value pairs with confidence.
JSON Syntax Quick Reference
Extension | .json |
Media Type | application/json |
Syntax Derived From | JavaScript Object Notation |
Comments Allowed? | No |
Encodings Supported | UTF-8 |
Standard | RFC 8259 |
Quick Fact:
JSON Syntax Rules
- Data is in name/value pairs: a key (double-quoted string), a colon, then the value.
- Pairs are separated by commas, but no trailing commas allowed.
- Curly braces { } denote JSON objects.
- Square brackets [ ] denote JSON arrays.
- Keys must always be double-quoted strings.
- Values must be properly formatted according to their respective data types.
- No comments allowed — they cause parse_error 101.
JSON Key-Value Pair Structure
In JSON, every piece of data is defined by a key and its corresponding value. The key must be enclosed in double quotes, followed by a colon, then the value.
JSON Syntax Example
{ "name": "Alice" }
JSON vs JavaScript Objects
JSON sturcutre resembles JavaScript objects, making it easy to understand but it follows strict rules that differ from JavaScript object notation.
Key Syntax Difference
In JSON, all keys must be enclosed in double quotes.
JSON
{ "userName": "john123" }
In contrast, JavaScript object keys can be unquoted identifiers, numeric values, or strings in either single or double quotes.
JavaScript
{ userName: "john123" }
JSON Values
JSON values can be one of the following data types:
- string
- number
- boolean (true / false)
- null
- object
- array
JavaScript Values
In JavaScript, values can include all JSON types and more:
- string
- number (including NaN, Infinity)
- boolean (true / false)
- null
- undefined
- object
- array
- function
- symbol
- bigint
Important:
JSON Array Syntax
An array holds an ordered list of values. Remember:
- Enclose values in [ ].
- Separate each item with a comma.
- Do not include a trailing comma after the last item.
Valid JSON Array
{ "tags": ["news", "updates", "events"] }
Invalid JSON Array
{ "tags": ["news", "updates", "events",] }
JSON Object Syntax
An object is a collection of key/value pairs. Keys must be quoted and values follow the same rules above.
Valid JSON Object
{ "product": { "id": 101, "name": "Widget", "price": 9.99 } }
Invalid JSON Object
{ 'product': { // Single quotes used "id": 101, "name": "Widget", "price": 9.99, "available": true, } }
Common JSON Syntax Errors and Debugging
Syntax Errors
These are mistakes made while writing or authoring JSON code.
- Missing quotes around keys or strings.
- Trailing commas after last item in arrays or objects.
- Unescaped control characters or improper Unicode sequences.
- Using single quotes instead of double quotes for keys or strings.
- Invalid escape sequences (e.g., \x instead of \u for Unicode characters).
- Unterminated strings or strings spanning multiple lines without proper escaping.
- Missing or mismatched brackets {} or [].
- Nested objects or arrays not properly formatted.
Parse Errors
The parse errors occur when the JSON code is being executed or loaded at runtime and can't be interpreted correctly, often resulting in errors when using JSON.parse() to convert the JSON string into a JavaScript object.
- Including comments or functions in JSON files (→ parse_error 101).
- Duplicate keys in objects.
- Invalid data types (e.g., using undefined or NaN).
- Unexpected end of JSON input.
- JSON string is not a valid JSON format (e.g., due to incorrect formatting or encoding issues).
Best Practices for Avoiding Errors
- Use a JSON linter or formatter like JSONLint or Prettier to maintain proper structure.
- Validate your JSON data against a schema (e.g., JSON Schema) whenever applicable.
- Integrate automated tests that parse and verify JSON payloads during development.
Debugging Tips
- Carefully read error messages that include line and column numbers to pinpoint the issue.
- Use language-specific tools like try { JSON.parse() } catch(e) { } in JavaScript to catch errors.
- Utilize browser dev tools or extensions that highlight JSON issues in network responses.
Performance Considerations
- Parsing very large JSON documents can cause performance issues or memory bloat.
- Use streaming parsers for large datasets instead of loading the full document into memory.