JSON Syntax
15 April 2025 | Category: JavaScript
JSON (JavaScript Object Notation) syntax is a set of rules that define how data is structured in JSON format. It is a lightweight, text-based format that is used to store and transfer data, especially between a client and a server.
The syntax is derived from JavaScript object literals, but JSON is a string format that is language-independent.
📜 JSON Syntax Rules
- Data is written as key/value pairs.
- Keys must be strings, written inside double quotes.
- Values can be:
- String
- Number
- Boolean
- Null
- Object (another JSON object)
- Array
- Objects are wrapped in
{ }
. - Arrays are wrapped in
[ ]
. - Commas separate pairs or elements.
- JSON does not support comments.
✅ JSON Syntax Example
{
"name": "John",
"age": 28,
"isEmployed": true,
"skills": ["HTML", "CSS", "JavaScript"],
"address": {
"city": "Mumbai",
"zip": "400001"
},
"spouse": null
}
🔍 Explanation of Syntax
Element | Type | Description |
---|---|---|
"name" | String | Must be in double quotes |
28 | Number | No quotes required |
true | Boolean | Must be lowercase (true or false ) |
["HTML",...] | Array | List of values in square brackets |
{...} | Object | Nested object inside the main object |
null | Null | Represents an empty value |
❌ Invalid JSON Examples
Here are some examples that will result in errors:
1. ❌ Single Quotes
{ 'name': 'John' }
✅ Use double quotes: { "name": "John" }
2. ❌ Trailing Commas
{
"name": "John",
"age": 28,
}
✅ Remove the last comma.
3. ❌ Comments
{
"name": "John" // This is not allowed
}
✅ JSON does not support comments. Remove them.
🧠 Summary
- JSON syntax is strict and simple.
- It is based on key/value pairs.
- Use double quotes for all keys and string values.
- Values must be valid types: string, number, object, array, boolean, or null.
- Do not include functions or comments in JSON.