Why JSON Exists

Your program's data lives in memory and disappears when it stops. To keep it, or to send it to another program, it has to become text.

JSON (JavaScript Object Notation) is the agreed way to write structured data as text. Nearly every website, app, and API on earth speaks it. It looks almost exactly like a JavaScript object or a Python dictionary, which is why it feels familiar immediately.

JSON supports exactly six kinds of value: objects {}, arrays [], strings, numbers, booleans, and null. No functions, no dates, no comments. That deliberate smallness is why every language can read it.

Data to JSON text

PYTHON

import json

record = {"name": "Ama", "hours": 95}
text = json.dumps(record)

print(text)
print(type(text))

JAVASCRIPT

const record = { name: "Ama", hours: 95 };
const text = JSON.stringify(record);

console.log(text);
console.log(typeof text);
Compare → Python calls it dumps, meaning dump-to-string. JavaScript calls it stringify. Both turn live data into a string of text you could save to a file or send over a network.
EXAMPLE
loading editor…
YOUR TURN
Your turn → Make a record about a book you like, with a title, a year and whether you have finished it. Turn it into JSON text and print it.
loading editor…

Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Reading JSON Back →
← Back