Advanced JSON Syntax Reference
JSON (JavaScript Object Notation) is a lightweight data interchange format that's easy for humans to read and write, and easy for machines to parse and generate. Below is a comprehensive guide to JSON syntax.
1. Basic Syntax
JSON Structure
- Objects: Key/value pairs enclosed in curly braces `{}`.
- Arrays: Ordered list of values enclosed in square brackets `[]`.
- Values: Can be a string, number, object, array, true, false, or null.
- Whitespace: Insignificant and can be used for readability.
Example
{
"name": "John Doe",
"age": 30,
"isStudent": false,
"address": {
"street": "123 Main St",
"city": "Anytown"
},
"courses": ["Math", "Science"],
"scores": {
"Math": 90,
"Science": 95
},
"spouse": null
}
2. Objects
Syntax
Example
{
"key1": "value1",
"key2": "value2",
"nestedObject": {
"nestedKey": "nestedValue"
}
}
3. Arrays
Syntax
- Enclosed in square brackets `[]`.
- Contains zero or more values.
- Values are separated by commas `,`.
Example
[
"value1",
"value2",
{
"key": "value"
},
[1, 2, 3]
]
4. Values
Types
- String: Enclosed in double quotes `" "`.
- Number: Integer or floating-point.
- Object: As defined above.
- Array: As defined above.
- Boolean: `true` or `false`.
- Null: `null`.
String Example
{
"greeting": "Hello, World!"
}
Number Example
{
"integer": 42,
"float": 3.14159
}
Boolean Example
{
"isTrue": true,
"isFalse": false
}
Null Example
{
"nothing": null
}
5. Strings
Syntax
- Enclosed in double quotes `" "`.
- Can contain Unicode characters and escape sequences.
Escape Sequences
- `\"` - Double quote
- `\\` - Backslash
- `\/` - Slash
- `\b` - Backspace
- `\f` - Form feed
- `\n` - Newline
- `\r` - Carriage return
- `\t` - Tab
- `\uXXXX` - Unicode character
Example
{
"text": "This is a string with a newline\nand a tab\tcharacter."
}
6. Numbers
Syntax
- Can be integer or floating-point.
- No quotes around numbers.
- Can include a negative sign `-`.
- Can use scientific notation `e` or `E`.
Example
{
"integer": 100,
"negativeInteger": -100,
"float": 3.14,
"negativeFloat": -3.14,
"scientific": 1.23e10
}
7. Nested Structures
Objects within Objects
{
"outerObject": {
"innerObject": {
"key": "value"
}
}
}
Arrays within Arrays
[
[1, 2, 3],
[4, 5, 6]
]
Mixed Nested Structures
{
"objectWithArray": {
"array": [1, 2, 3]
},
"arrayWithObjects": [
{"key1": "value1"},
{"key2": "value2"}
]
}
8. Comments
JSON does not support comments. Avoid adding comments within JSON files.
9. Special Characters and Unicode
Special Characters
- Use escape sequences for special characters as mentioned above.
Unicode Characters
- Represent Unicode characters with `\u` followed by a 4-hexadecimal digit code.
Example
{
"emoji": "\uD83D\uDE00", // 😀
"currency": "\u0024" // $
}
10. Best Practices
- Consistency: Maintain consistent use of spaces and indentation.
- Readability: Use whitespace to improve readability.
- Key Naming: Use meaningful and consistent key names.
- Avoid Trailing Commas: Ensure no trailing commas in objects or arrays to maintain compatibility.
Example
{
"user": {
"id": 1,
"name": "Alice",
"email": "alice@example.com"
},
"active": true,
"roles": ["admin", "user"]
}
11. Common Mistakes
Missing Quotes around Keys
Incorrect:
{
name: "John Doe"
}
Correct:
{
"name": "John Doe"
}
Trailing Commas
Incorrect:
{
"name": "John Doe",
}
Correct:
{
"name": "John Doe"
}
Single Quotes for Strings
Incorrect:
{
'name': 'John Doe'
}
Correct:
{
"name": "John Doe"
}
12. Validating JSON
Online Validators
- Use online tools like JSONLint to validate and format JSON.JSON Schemas
- Define JSON structures using JSON Schema to validate data against a predefined format.
Example Schema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Person",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer",
"minimum": 0
},
"email": {
"type": "string",
"format": "email"
}
},
"required": ["name", "age"]
}
By following this reference, you can effectively create, read, and validate JSON data, ensuring it adheres to proper syntax and best practices.
www.json.org