Data as text
An object lives in a program's memory. To save it or send it to another program, it must become text. JSON, short for JavaScript Object Notation, is a simple text format for data that almost every language can read.
Object to text
The curly braces make an object: a group of named values called properties. This one has two: name holds the string "Tom" and age holds the number 3. The variable cat refers to it.
JSON.stringify(cat) walks through the object and writes it out as text: an opening brace, each property as "key":value with a comma between them, then a closing brace. The result is one string, 22 characters long.
JSON is strict. Every key and every string sits in double quotes. Values can only be strings, numbers, true, false, null, arrays and objects. JSON.parse rejects text that breaks the rules with a SyntaxError.
console.log(text) prints the string in the console. It looks a lot like the object, but it is only characters now: text.name is undefined, because a string has no name property.
Text to object
The object only exists while this program runs. Text can leave it: save it in a file such as cat.json, or send it over the network to a server. Another program, in any language, can read it back.
JSON.parse(text) reads the characters and builds a brand-new object from them, with the same properties: name "Tom" and age 3. The variable copy refers to it. It is a second object, not cat.
copy.age = 4 puts 4 in the age property of copy. Only copy changes. cat still says 3, because the two objects only shared some text, never the same memory.
console.log(cat.age, copy.age) prints 3 4. Object to text with JSON.stringify, text back to a new object with JSON.parse: that round trip is how programs save and share data.
What happens
// step 1/9 · json
JSON JavaScript Object Notation
is plain text, written in a strict format
used to save data in files (.json)
send data between programs and computers
in JS JSON.stringify value → text
JSON.parse text → value
standard ECMA-404, RFC 8259
JSON.stringify writes no spaces unless you ask: JSON.stringify(cat, null, 2) writes the same data over several lines, indented by two spaces. Both are valid JSON. The boxes in memory are a simplification: cat and copy are variables that each refer to an object; the objects hold the properties.