What You'll Learn
- What JSON is and why it is everywhere
- The six data types JSON supports
- How to read and write JSON
- Why JSON beat XML for APIs
Why This Matters
Almost every modern API sends and receives data as JSON. If you cannot read JSON, you cannot read API responses. If you cannot write JSON, you cannot send API requests. This lesson gives you the format in one sitting.
Simple Explanation
JSON (JavaScript Object Notation) is a way to write data as plain text so that both humans and computers can read it. It looks like this:
{
"name": "Anita",
"age": 28,
"is_active": true,
"tags": ["vip", "early-adopter"],
"address": null
}
That is JSON. It is just text — but it has a strict structure that every programming language can parse.
Real-World Analogy — A Filled-Out Form
Think of a job application form. Each field has a label ("Name", "Age", "Email") and a value you fill in. JSON works the same way: each piece of data has a label (called a key) and a value.
The form is structured — you don't write your age in the "Name" field. JSON enforces the same structure: keys and values are paired, and the whole thing is wrapped in curly braces.
The Six JSON Data Types
| Type | Example | Notes |
|---|---|---|
| String | "Anita" | Always in double quotes |
| Number | 28 or 3.14 | Integer or decimal; no quotes |
| Boolean | true or false | Lowercase, no quotes |
| Null | null | Lowercase, no quotes; means "no value" |
| Array | [1, 2, 3] | Ordered list, in square brackets |
| Object | {"k": "v"} | Key-value pairs, in curly braces |
Rules You Must Follow
- Keys must be in double quotes.
{"name": "Anita"}is valid;{name: "Anita"}is not (in strict JSON). - Strings must be in double quotes. Single quotes are not allowed:
'Anita'is invalid. - No trailing commas.
{"a": 1, "b": 2,}is invalid — remove the last comma. - No comments.
// this is a commentis not allowed in JSON. - Boolean and null are lowercase.
True,TRUE,Null,NULLare all invalid.
Reading JSON — Step by Step
{
"id": 123,
"name": "Anita",
"email": "anita@example.com",
"is_active": true,
"roles": ["admin", "editor"],
"address": {
"city": "Mumbai",
"country": "India"
},
"phone": null
}
Read it as:
idis a number: 123nameis a string: "Anita"emailis a string: "anita@example.com"is_activeis a boolean: truerolesis an array of two stringsaddressis an object with two keys (city, country)phoneis null (no value provided)
Why JSON Beat XML
Before JSON, the standard was XML. XML works but is verbose:
<user>
<id>123</id>
<name>Anita</name>
<email>anita@example.com</email>
</user>
The same data in JSON:
{"id": 123, "name": "Anita", "email": "anita@example.com"}
JSON is smaller, faster to parse, and easier to read. XML is still used in some enterprise and SOAP APIs (covered in Module 12), but JSON is the default for new APIs.
Common Mistakes
- Using single quotes.
{'name': 'Anita'}works in JavaScript but is invalid JSON. - Trailing commas. Most parsers reject them. JSON validators flag them.
- Comments in JSON. If you need comments, use a separate documentation file or JSONC (a non-standard extension).
- Unquoted keys.
{name: "Anita"}is JavaScript, not JSON. - Using
NaNorInfinity. JSON has no representation for these. Use null instead.
Practical Exercise (5 minutes)
Copy this JSON into a text file and try to identify all the data types:
{
"product": "Laptop",
"price": 95000,
"in_stock": true,
"tags": ["electronics", "computers"],
"discount": null,
"specs": {"ram": 16, "ssd": 512}
}
Answers: product=string, price=number, in_stock=boolean, tags=array, discount=null, specs=object.
Mini Challenge
Write valid JSON for a "book" with these fields: title, author, year, price, in_print (boolean), genres (array of strings), and publisher (object with name and country). Validate your JSON at jsonlint.com.
Key Takeaways
- JSON is plain-text data with a strict structure — readable by humans and machines.
- Six types: string, number, boolean, null, array, object.
- Keys and strings use double quotes; no trailing commas; no comments.
- JSON is the default format for modern APIs because it is small, fast, and easy to parse.
Previously: Module 1 (lessons 01–10) covered API fundamentals.
Today: You learned JSON — the data format you'll meet in almost every API.
Next: Lesson 12 goes deeper into JSON objects, arrays, and nested structures.
FAQ
Is JSON only for JavaScript?
No. The name is historical — JSON was invented in JavaScript, but every modern programming language can read and write it. Python's json module, Java's Jackson library, Go's encoding/json, and PHP's json_encode all work with the same JSON format.
What is the difference between JSON and a JavaScript object?
JSON is a text format. A JavaScript object is a runtime data structure. JSON requires double-quoted keys and string values; JavaScript objects allow unquoted keys, single quotes, functions, and more. All JSON is valid JavaScript (via JSON.parse), but not all JavaScript object syntax is valid JSON.
Comments
Comments
Post a Comment