Beginner · How the web and backends work · topic 6 of 54 · 25 min
JSON: the language of APIs
Explanation
What JSON is
JSON is plain text that holds data in a fixed, simple shape. It looks like a JavaScript object, but it is only text. Because it is text, any language (JavaScript, Python, Java, Go) can read and write it. That is why APIs use it.
{
"name": "Asha Verma",
"age": 24,
"isStudent": true,
"skills": ["JavaScript", "SQL"],
"address": { "city": "Indore", "pin": "452001" },
"referral": null
}The six types
| Type | Example | Note |
|---|---|---|
| string | "Pune" | Always double quotes |
| number | 42, 99.5, -3 | No quotes; no NaN or Infinity |
| boolean | true, false | Small letters |
| null | null | Means "no value" |
| array | [1, 2, 3] | An ordered list |
| object | {"key": "value"} | Keys must be strings in double quotes |
stringify and parse
JSON.stringify(value) turns a JavaScript value into JSON text, ready to send. JSON.parse(text) does the opposite: it reads JSON text and gives you a real object you can use.
const order = { item: 'Paneer roll', qty: 2, paid: true };
const text = JSON.stringify(order);
console.log(text); // {"item":"Paneer roll","qty":2,"paid":true}
console.log(typeof text); // string
const back = JSON.parse(text);
console.log(back.qty * 60); // 120
console.log(JSON.stringify(order, null, 2)); // pretty, with 2-space indentCommon mistakes
- Single quotes: {'name': 'Ravi'} is not JSON. Use double quotes.
- Trailing comma: {"a": 1,} is not JSON. Remove the last comma.
- Comments: // and /* */ are not allowed in JSON.
- Keys without quotes: {name: "Ravi"} is a JavaScript object, but not JSON.
- undefined and functions are skipped by JSON.stringify; dates become strings.
function readSettings(text) {
try {
return JSON.parse(text);
} catch (error) {
console.log('Bad JSON, using defaults:', error.name);
return { theme: 'light' };
}
}
console.log(readSettings('{"theme": "dark"}')); // { theme: 'dark' }
console.log(readSettings("{'theme': 'dark'}")); // prints a SyntaxError note, then { theme: 'light' }Watch the lesson
JSON: the language of APIs · 1/6
JSON is a simple text format for data. Almost every API speaks JSON.
Prefer a video by a teacher? Find free videos on “json explained for beginners javascript parse stringify” on YouTube ↗
In real life: A UPI payment status from a payment API
When a payment finishes, the payment company calls your backend with a JSON body like the one below. Your server parses it, checks the status and amount, and marks the order paid. If the text is broken, your server must not crash; it answers 400 and logs the problem.
const body = '{"orderId": "ORD-7781", "amount": 349, "status": "SUCCESS", "upiRef": "412345678901"}';
let payment;
try {
payment = JSON.parse(body);
} catch (error) {
payment = null;
}
if (payment && payment.status === 'SUCCESS') {
console.log('Mark ' + payment.orderId + ' as paid: Rs ' + payment.amount);
} else {
console.log('Answer 400: bad or failed payment data');
}Practice (0/3 done)
Write a function safeParse(text) that returns { ok: true, value } when text is valid JSON, and { ok: false, error } when it is not, where error is the error message (a string). It must never throw.
Write a function jsonResponse(status, data) that returns a response object { status, headers: { "Content-Type": "application/json" }, body } where body is data turned into JSON text.
A kirana app sends a bill as JSON text: a list of items like [{"item": "Milk", "price": 30, "qty": 2}]. Write a function billTotal(text) that returns the total (price times qty, added up). If the text is not valid JSON or not a list, return 0.
Quick quiz
Read more (free)
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.