JSON is a plain-text way to write down data so it can be saved in a file or sent across the internet. It looks a lot like a JavaScript object, which is no accident, but it is only text until a program reads it. This section covers how to write it, read it, create it, load it from a server, and show it on a page.
JSON stands for JavaScript Object Notation. Its look was borrowed from the object literals you already write, but it is a separate format with its own strict rules. Python, Java, PHP, and almost every other language can read and write it, which is why it became the standard way for programs to swap data.
{
"name": "Ava",
"grade": 10,
"clubs": ["robotics", "chess"],
"active": true
}
That is one JSON document: a student with a name, a grade, a list of clubs, and a yes-or-no flag. Anyone can read it, and so can a computer.
The most important idea in this section: JSON is a string. Until a program parses it, it has no properties, no methods, and nothing you can reach with a dot. It is just characters.
const text = '{"name":"Ava","grade":10}';
console.log(typeof text);
console.log(text.length);
console.log(text.name);
Single quotes wrap the whole string because the JSON inside needs double quotes around every key (the next lesson explains why). text.name is undefined: a string has no property called name.
Two built-in functions on the JSON object move data in each direction:
| Function | Takes | Gives back |
|---|---|---|
| JSON.parse(text) | JSON text | a JavaScript value (object, array, number...) |
| JSON.stringify(value) | a JavaScript value | JSON text |
const student = { name: "Ava", grade: 10 };
const text = JSON.stringify(student);
console.log(text);
const copy = JSON.parse(text);
console.log(copy.name, copy.grade);
The two lessons after the syntax rules cover each function in full. For now, remember the direction: parse reads text into a value, stringify writes a value out as text.
JSON borrowed the look of an object literal but is stricter. Something that is perfectly good JavaScript can be broken JSON:
| JavaScript object | JSON | |
|---|---|---|
| Keys | can be unquoted: name: 1 | must be double-quoted: "name": 1 |
| Strings | single or double quotes | double quotes only |
| Trailing comma | allowed | an error |
| Functions, undefined | allowed as values | not allowed |
| Comments | allowed | not allowed |
| What it is | live code in memory | text you can save or send |
{ name: 'Ava', }
The line above is a valid JavaScript object but is not valid JSON: the key is unquoted, the string uses single quotes, and there is a comma after the last item. A JSON parser would reject all three.
{"a": 1} typed straight into your code is an object. '{"a": 1}' with quotes around it is a string that happens to contain JSON.
package.json keep their options as JSON.json fileA JSON file ends in .json and is served with the type application/json, so browsers and programs know how to treat it.
Real JSON usually nests: objects inside arrays inside objects. Here a class holds a list of students, and each student is an object:
const text = `{"class":"Coding 1","students":[{"name":"Ava","score":92},{"name":"Ben","score":85}]}`;
const data = JSON.parse(text);
console.log(data.class);
console.log(data.students.length);
console.log(data.students[1].name);
After JSON.parse the result is an ordinary object, so everything you learned in the Objects and Arrays sections applies: dots, brackets, loops, and array methods.
Two programs on different computers cannot hand each other a live object, because an object only exists inside one running program's memory. They have to agree on a written form instead. JSON won because people can read it, it is small, and every language can parse it.
| Lesson | Question it answers |
|---|---|
| JSON Syntax | What are the exact rules? |
| JSON Values | Which kinds of values are allowed? |
| JSON Parse | How do I turn JSON text into a value? |
| JSON Stringify | How do I turn a value into JSON text? |
| JSON Fetch | How do I load JSON from a server? |
| JSON HTML | How do I show JSON data on a page? |
| JSON vs XML | How does JSON compare with the older format? |
Which statement about JSON is true?
numberWhat does this log?
const s = '{"a": 1}';
console.log(s.a);
undefined. s is a string, and a string has no property called a. Parse it first: JSON.parse(s).a is 1.Name the two functions that convert between JSON text and JavaScript values, and say which direction each goes.
JSON.parse turns JSON text into a value. JSON.stringify turns a value into JSON text.