Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
API Basics APIs APIs From Zero to Real-World API Testing & Automation JSON Module 2 — JSON and Data

What Is JSON? The Data Format of Modern APIs

Reviewed & accurate
AI Summary

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

TypeExampleNotes
String"Anita"Always in double quotes
Number28 or 3.14Integer or decimal; no quotes
Booleantrue or falseLowercase, no quotes
NullnullLowercase, no quotes; means "no value"
Array[1, 2, 3]Ordered list, in square brackets
Object{"k": "v"}Key-value pairs, in curly braces
Standard reference: JSON is defined by RFC 8259. The official site is json.org.

Rules You Must Follow

  1. Keys must be in double quotes. {"name": "Anita"} is valid; {name: "Anita"} is not (in strict JSON).
  2. Strings must be in double quotes. Single quotes are not allowed: 'Anita' is invalid.
  3. No trailing commas. {"a": 1, "b": 2,} is invalid — remove the last comma.
  4. No comments. // this is a comment is not allowed in JSON.
  5. Boolean and null are lowercase. True, TRUE, Null, NULL are 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:

  • id is a number: 123
  • name is a string: "Anita"
  • email is a string: "anita@example.com"
  • is_active is a boolean: true
  • roles is an array of two strings
  • address is an object with two keys (city, country)
  • phone is 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

  1. Using single quotes. {'name': 'Anita'} works in JavaScript but is invalid JSON.
  2. Trailing commas. Most parsers reject them. JSON validators flag them.
  3. Comments in JSON. If you need comments, use a separate documentation file or JSONC (a non-standard extension).
  4. Unquoted keys. {name: "Anita"} is JavaScript, not JSON.
  5. Using NaN or Infinity. 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.
Course continuity
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.

Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments