JSON
Data as text.
When data travels between computers, from a server to your browser, or into a file, it travels as text. The most popular format for that text is JSON, JavaScript Object Notation. It looks almost exactly like JavaScript objects.

- Data travels as text
- Server to browser, files
- JSON
stringify and parse
JSON dot stringify turns an object into a JSON string. The extra arguments add indentation, to make it readable. JSON dot parse goes the other way, turning a JSON string back into a real object you can use. Run it.
const settings = { theme: "dark", fontSize: 16, tags: ["a", "b"] };
const text = JSON.stringify(settings);
console.log(text, typeof text);
console.log(JSON.stringify(settings, null, 2));
const back = JSON.parse(text);
console.log(back.theme, typeof back);{"theme":"dark","fontSize":16,"tags":["a","b"]} string
{
"theme": "dark",
"fontSize": 16,
"tags": [
"a",
"b"
]
}
dark objectJSON rules
JSON is stricter than JavaScript. Keys must be in double quotes, strings must use double quotes, and there are no trailing commas or comments. It can't store functions, or undefined. If you try to parse broken JSON, you get an error, so wrap parse in try and catch when the text comes from outside.
| key | value |
|---|---|
| JSON.stringify | object → text |
| JSON.parse | text → object |
| Strict | double quotes, no comments |
| Can't store | functions, undefined |
Summary
JSON is how data travels as text. Stringify turns objects into text, and parse turns text back into objects. Next, copying objects safely.
Key takeaways- JSON.stringify
- JSON.parse
- Strict format