Why JSON exists
Objects live in your program's memory. To save one to a file or send it over the internet, you need it as text. That text format is JSON (JavaScript Object Notation), and it looks almost exactly like the object syntax you just learned. You met it in Python as the json module.
Two built-in converters, both on the JSON object:
| Direction | JavaScript | Python equivalent |
|---|---|---|
| object → text | JSON.stringify(obj) | json.dumps(obj) |
| text → object | JSON.parse(text) | json.loads(text) |
JSON is stricter than JavaScript itself: keys must be double-quoted, strings must use double quotes, and there are no comments and no trailing commas. true, false, and null are lowercase, which finally matches what you learned in lesson 2-2.
A full round trip through text and back
The two converters used one after the other. The object becomes text, the text is confirmed to really be text, and then it is rebuilt into a live object whose nested array can be indexed again.
const order = { id: 7, items: ["mouse", "keyboard"], paid: false }; const text = JSON.stringify(order); console.log(text); console.log(typeof text); const back = JSON.parse(text); console.log(back.items[1]);
Output
{"id":7,"items":["mouse","keyboard"],"paid":false}
string
keyboardThe printed JSON shows what stringify produces: every key wrapped in double quotes, no spaces, and false in lowercase. The typeof check confirms the result is one long string, which is the point of the exercise, since a string is the only thing a file or a network connection can carry.
After JSON.parse, back is an ordinary object again, with real nesting restored. back.items[1] proves it by reaching into the rebuilt array and pulling out "keyboard".
Parsing a response that arrived as text
Data from a server arrives as a string, exactly like raw below. Nothing can be read out of it until JSON.parse turns it back into an object.
const raw = '{"name":"Grace","languages":["COBOL","JavaScript"]}'; const person = JSON.parse(raw); console.log(person.name); console.log(person.languages.length);
Output
Grace
2Notice the quoting on the first line. The whole JSON text sits inside single quotes precisely because the JSON itself is full of double quotes, and using single quotes on the outside avoids escaping every one of them.
After parsing, person is a normal object with no trace of its text origin, so person.name and person.languages.length work exactly as they would on an object you had typed by hand.
JSON.parsethrows an error when the text is not valid JSON, and truncated or misquoted responses are common in real systems. Lesson 8-3 introducestry/catch, which is how production code wraps a parse it does not fully trust.
Preparing an object to travel
Sending an object over the network starts with JSON.stringify(obj), because objects must become text before they can travel.
Networks and files carry bytes of text, not live objects, so the conversion is unavoidable. JSON.parse is the same trip in the other direction, turning received text back into an object, which makes the two functions a matched pair rather than alternatives.
One tempting shortcut is worth naming as a mistake. String(obj) also produces a string, but it produces "[object Object]", a fixed piece of text containing none of your data. Seeing that value in a log line or a request body is a reliable sign that a JSON.stringify call was missed somewhere upstream.