JSON Syntax Rules
What is JSON Syntax?
Syntax means the rules that must be followed when writing something. Just like English has grammar rules, JSON has its own set of rules that must be followed exactly. If even one rule is broken, the JSON becomes invalid and cannot be read by any application.
Understanding JSON syntax is essential because a single misplaced comma or a missing quotation mark can break an entire application.
Rule 1 — Data is Written in Key-Value Pairs
Every piece of data in JSON is written as a combination of a key and a value, separated by a colon :.
"country": "India"Here, "country" is the key and "India" is the value. The key is always on the left and the value is always on the right.
Rule 2 — Keys Must Always Be Strings in Double Quotes
In JSON, every key must be a string, and that string must be wrapped in double quotes. Single quotes are not allowed.
Correct:
"city": "Mumbai"Wrong (single quotes):
'city': 'Mumbai'Wrong (no quotes):
city: "Mumbai"Rule 3 — Data Pairs Are Separated by Commas
When there are multiple key-value pairs, each pair is separated by a comma. However, the last pair should NOT have a trailing comma.
Correct:
{
"name": "Priya",
"age": 25,
"city": "Delhi"
}Wrong (trailing comma after last item):
{
"name": "Priya",
"age": 25,
"city": "Delhi",
}Rule 4 — Curly Braces Hold Objects
A JSON object is wrapped inside curly braces { }. An object is a collection of key-value pairs.
{
"product": "Laptop",
"price": 45000
}Rule 5 — Square Brackets Hold Arrays
A JSON array is a list of values wrapped inside square brackets [ ]. Arrays are used when there are multiple values for a single key.
{
"colors": ["red", "green", "blue"]
}Rule 6 — String Values Must Be in Double Quotes
Any value that is text (a string) must be written inside double quotes. Numbers, booleans, and null do not need quotes.
Correct:
"language": "Hindi"Wrong (single quotes):
"language": 'Hindi'Rule 7 — JSON is Case-Sensitive
JSON treats uppercase and lowercase letters differently. The key "Name" and the key "name" are two completely different keys.
{
"name": "Amit",
"Name": "Sharma"
}In the above example, both keys are considered separate and unique.
Rule 8 — No Comments Allowed
JSON does not support comments. Adding a comment like // this is a name inside JSON data will make it invalid. This is one of the most common mistakes beginners make.
Wrong (comments not allowed):
{
// Student details
"name": "Neha",
"age": 20
}Rule 9 — Whitespace is Ignored
Spaces, tabs, and new lines between JSON elements are completely ignored. They are only used to make JSON look neat and readable. Both versions below are valid and identical:
Compact (valid):
{"name":"Raj","age":30}Formatted (also valid):
{
"name": "Raj",
"age": 30
}Complete Valid JSON Example
Below is a complete, valid JSON object that follows all the rules described above:
{
"studentName": "Ankita Singh",
"rollNumber": 105,
"subjects": ["Math", "Science", "English"],
"isPassed": true,
"address": null
}Common JSON Syntax Mistakes
| Mistake | Wrong Example | Correct Example |
|---|---|---|
| Single quotes on keys | 'name': 'John' | "name": "John" |
| No quotes on key | name: "John" | "name": "John" |
| Trailing comma | "age": 25, (last item) | "age": 25 (no comma at end) |
| Adding comments | // student info | Not allowed in JSON |
Summary
JSON syntax is strict but simple. Always use double quotes for keys and string values, separate pairs with commas, avoid trailing commas, never add comments, and wrap objects in curly braces and arrays in square brackets. Following these rules ensures that JSON data is always valid and can be used by any application.
