Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

API
APIs: REST & GraphQL
0/11 completed
Beginner
What is an API?HTTP methods, status codes and headersJSON
Intermediate
REST design principlesErrors and validationPagination, filtering and sortingGraphQL basics
Advanced
Versioning and cachingRate limiting and idempotencyOpenAPI documentation
Pro
Webhooks, real-time APIs and API security
Tracks / APIs: REST & GraphQL / Beginner

JSON

JSON (JavaScript Object Notation) is the data format of web APIs. It's plain text, easy for humans to read and for every programming language to parse. If you've written a JavaScript object, you already almost know it.

The syntax

json
{
  "id": 42,
  "name": "Wireless Mouse",
  "price": 24.99,
  "inStock": true,
  "discount": null,
  "tags": ["electronics", "accessories"],
  "dimensions": { "width": 6.2, "height": 3.8, "unit": "cm" },
  "reviews": [
    { "user": "asha", "stars": 5 },
    { "user": "rahul", "stars": 4 }
  ]
}

JSON has exactly six types:

Type Example
string "hello" (always double quotes)
number 42, -3.5, 1e6
boolean true, false
null null
array [1, 2, 3]
object {"key": "value"} (keys are always double-quoted strings)

Common mistakes

JSON is stricter than JavaScript:

json
{ name: "Asha" }              ✖ keys need double quotes
{ 'name': 'Asha' }            ✖ single quotes aren't allowed
{ "name": "Asha", }           ✖ no trailing commas
{ "created": new Date() }     ✖ no functions, dates or undefined
// comments                   ✖ JSON has no comments

JSON in JavaScript

JSON.stringify turns a value into JSON text; JSON.parse turns JSON text back into a value:

Node.js · editable

What doesn't survive the trip

Node.js · editable
  • Dates become ISO 8601 strings ("2026-11-20T09:30:00.000Z"). That's also the format to use in your APIs: unambiguous and sortable. Always send times in UTC.
  • undefined and functions disappear; NaN and Infinity become null; Sets and Maps become {}.
  • Big numbers: JSON numbers are usually read as 64-bit floats, which can't hold integers above 2⁵³ exactly. Send large IDs (like Twitter's) and money amounts with many digits as strings, or store money as integer cents.

Invalid JSON

JSON.parse throws on invalid input. Any server parsing a request body must handle that:

Node.js · editable

Customising the output

Node.js · editable

Removing sensitive fields before sending is important, but don't rely on remembering it: build response objects explicitly ({ id, name }) so new database columns are never leaked by accident.

JSON in Python

Every language has a JSON library. Python's is built in:

Python · editable

API conventions

  • Send Content-Type: application/json and use UTF-8.
  • Pick one naming style and stick to it. camelCase (createdAt) is common in JavaScript APIs, snake_case (created_at) in Python and Ruby ones.
  • Return objects at the top level, not bare arrays ({"data": [...]} instead of [...]), so you can add fields like pagination info later without breaking clients.
  • Use null for "no value" consistently, rather than sometimes leaving the field out.
  • JSON Lines (.jsonl, one JSON object per line) is handy for logs and streaming many records.

Practice

  1. Write the JSON for a blog post with a title, an author object, a list of tags and a publishedAt date.
  2. Write a function safeParse(text) that returns { ok: true, value } or { ok: false, error } instead of throwing.
  3. Give a User class a toJSON() method that only exposes id, name and email.
✎ Check your understanding

1.Which of these is valid JSON?

2.What does JSON.parse(JSON.stringify({ when: new Date() })) give you for when?

3.Why send very large IDs as strings?

← HTTP methods, status codes and headers