JSON (JavaScript Object Notation) is a widely used data format for storing and transferring data. Its simplicity and human-readable structure make it ideal for working with APIs, configuration files, databases, and more. This tutorial covers the core syntax rules of JSON, helping you understand how to create and interpret JSON data.
1. Basic Structure
JSON is built on two data structures:
- Objects: Unordered sets of name/value pairs, enclosed in
{}
. - Arrays: Ordered lists of values, enclosed in
[]
.
Here is a simple example that combines both:
jsonCopyEdit{
"name": "Alice",
"age": 25,
"languages": ["English", "French"]
}
2. JSON Data Types
JSON supports six data types:
Type | Example |
---|---|
String | "Hello" |
Number | 100 , 15.75 |
Object | { "key": "value" } |
Array | [1, 2, 3] |
Boolean | true , false |
Null | null |
Note: JSON does not support functions, dates, or undefined values natively.
3. JSON Objects
A JSON object is a collection of key-value pairs enclosed in curly braces {}
.
Syntax Rules:
- Keys must be strings in double quotes.
- Keys and values are separated by a colon
:
. - Each key-value pair is separated by a comma
,
.
Example:
jsonCopyEdit{
"firstName": "John",
"lastName": "Doe",
"age": 30
}
In this example:
"firstName"
is the key."John"
is the value.
4. JSON Arrays
A JSON array is an ordered list of values enclosed in square brackets []
.
Syntax Rules:
- Elements are separated by commas.
- Elements can be any valid JSON data type (string, number, object, array, boolean, or null).
Example:
jsonCopyEdit{
"colors": ["red", "green", "blue"]
}
An array can also include objects:
jsonCopyEdit{
"students": [
{ "name": "Alice", "age": 22 },
{ "name": "Bob", "age": 24 }
]
}
5. Nesting in JSON
JSON allows you to nest objects and arrays inside one another.
Example:
jsonCopyEdit{
"company": {
"name": "Tech Co",
"employees": [
{ "name": "Alice", "position": "Developer" },
{ "name": "Bob", "position": "Designer" }
]
}
}
Here:
- The outer object has a
company
key. - The value is another object that contains a nested array of employees.
6. Formatting and Validity
JSON Rules:
- Use double quotes (
"
) for all keys and string values. - No trailing commas (
,
). - No comments allowed.
- Whitespace (spaces, tabs, new lines) is allowed for formatting but ignored when parsing.
Invalid JSON (common mistakes):
jsonCopyEdit{
name: "Alice" // ❌ Keys must be in double quotes
}
jsonCopyEdit{
"name": "Alice",
} // ❌ Trailing comma
7. JSON Validation Tools
To check if your JSON is valid, use online validators like:
These tools can help you identify syntax errors quickly.
8. JSON in Programming
Most modern languages have built-in support for parsing and generating JSON.
JavaScript Example:
javascriptCopyEditconst jsonData = '{"name": "Alice", "age": 25}';
const obj = JSON.parse(jsonData); // Convert JSON string to object
const newJson = JSON.stringify(obj); // Convert object back to JSON string
Conclusion
JSON syntax is simple but powerful. With only a few rules to follow — such as using double quotes, separating key-value pairs with colons, and avoiding trailing commas — you can create structured, readable, and valid JSON. Mastering JSON syntax is essential for developers working with web APIs, data storage, and configuration files.
Would you like this turned into a practice worksheet, a quiz, or a downloadable handout?