Programs need an agreed way to describe data when they send it to each other. JSON has become the most common choice on the web, because it is short and easy to read.
What JSON Is
JSON, JavaScript Object Notation, is a text format for data. It looks like a JavaScript object, but it is only text, and it can be read by programs written in almost any language. A JSON file has the extension .json.
Definition: JSON. JavaScript Object Notation, a text format that stores data as keys and values.
Syntax Rules
A JSON object is written in braces. Inside are key-value pairs, separated by commas. Each key is a string in double quotes, followed by a colon and a value. The value can be a string, a number, true, false, null, an array or another object.
Definition: Key-value pair. A name in double quotes, a colon and a value, which is one item of a JSON object.
Table 4.7: JSON value types
| Type | Example | Note |
|---|---|---|
| String | "Asha" |
Always double quotes |
| Number | 20, 3.5 |
No quotes |
| Boolean | true |
Lower case |
| Null | null |
Empty value |
| Array | ["HTML", "CSS"] |
Square brackets |
| Object | { "city": "Gurugram" } |
Braces |
Arrays and Nesting
A JSON array is a list in square brackets, and its items are separated by commas. Objects and arrays can sit inside each other, so a file can hold a list of students, each with a name, marks and a list of courses.
JSON and JavaScript Objects
A JavaScript object can leave quotes off its keys and may use single quotes, comments and a final comma. JSON allows none of these. A trailing comma or a single-quoted key makes the whole file invalid.
Example 4.7: A student record
The record below has a string, a number and an array. Reading it with JSON.parse in Node.js shows the values:
const text = '{ "name": "Asha", "age": 20, "courses": ["HTML", "CSS"] }';
const student = JSON.parse(text);
console.log(student.name);
console.log(student.courses.length, student.courses[1]);
Output:
Asha
2 CSS
Common mistakes
- Using single quotes or leaving the quotes off a key, which is invalid in JSON.
- Leaving a trailing comma after the last pair.
- Writing comments inside a JSON file, which JSON does not allow.
- Writing numbers or true and false inside quotes, which turns them into strings.
Key takeaways
- JSON stores data as quoted keys and typed values.
- Objects use braces, and arrays use square brackets.
- The rules are strict: double quotes, no comments and no trailing commas.
- JSON is only text, and many languages can read it.
Practice questions
- What does JSON stand for?
- Which quotation marks are allowed around a key?
- Name three kinds of value a JSON field can hold.
Answers to the practice questions
- JavaScript Object Notation.
- Double quotation marks only.
- For example a string, a number and an array.
Sources
- Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit IV
