Skip to content

Commit 7e09cf7

Browse files
committed
Introduce JSON in Chapter 4
1 parent 22164ff commit 7e09cf7

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

04_data.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,8 @@ store this number at index 2 of the array.
586586

587587
{{index "phi coefficient", "phi function"}}
588588

589+
{{id phi_function}}
590+
589591
This is the function that computes the _ϕ_ coefficient from such an
590592
array:
591593

@@ -1176,6 +1178,68 @@ Note that if the value given to such a destructuring binding is `null`
11761178
or `undefined`, you get an error, much like you would if you'd
11771179
directly try to access a property of those values.
11781180

1181+
## JSON
1182+
1183+
{{index [array, representation], [object, representation], "data format"}}
1184+
1185+
Because properties only grasp their value, rather than containing it,
1186+
objects and arrays are stored in the computer's ((memory)) as
1187+
sequences of bits holding the _((address))es_—the place in memory—of
1188+
their contents. So an array with another array inside of it consists
1189+
of (at least) one memory region for the inner array, and another for
1190+
the outer array, containing (among other things) a binary number that
1191+
represents the position of the inner array.
1192+
1193+
If you want to save data in a file for later, or send it to another
1194+
computer over the network, you have to somehow convert these tangles
1195+
of memory addresses to a description that can be stored or sent. You
1196+
_could_ send over your entire computer memory along with the address
1197+
of the value you're interested in, I suppose, but that doesn't sound
1198+
like a good approach.
1199+
1200+
{{indexsee "JavaScript Object Notation", JSON}}
1201+
1202+
{{index serialization, "World Wide Web"}}
1203+
1204+
What we can do _serialize_ the data. That means it is converted into a
1205+
flat description. A popular format is called _((JSON))_ (pronounced
1206+
“Jason”), which stands for JavaScript Object Notation. It is widely
1207+
used as a data storage and communication format on the Web, even in
1208+
languages other than JavaScript.
1209+
1210+
{{index array, object, [quoting, "in JSON"], comment}}
1211+
1212+
JSON looks similar to JavaScript's way of writing arrays and objects,
1213+
with a few restrictions. All property names have to be surrounded by
1214+
double quotes, and only simple data expressions are allowed—no
1215+
function calls, bindings, or anything that involves actual
1216+
computation. Comments are not allowed in JSON.
1217+
1218+
A journal entry might look like this when represented as JSON data:
1219+
1220+
```{type: "application/json"}
1221+
{
1222+
"squirrel": false,
1223+
"events": ["work", "touched tree", "pizza", "running"]
1224+
}
1225+
```
1226+
1227+
{{index "JSON.stringify function", "JSON.parse function", serialization, deserialization, parsing}}
1228+
1229+
JavaScript gives us functions, `JSON.stringify` and `JSON.parse`, that
1230+
convert data to and from this format. The first takes a JavaScript
1231+
value and returns a JSON-encoded string. The second takes such a
1232+
string and converts it to the value it encodes.
1233+
1234+
```
1235+
let string = JSON.stringify({squirel: false,
1236+
events: ["weekend"]});
1237+
console.log(string);
1238+
// → {"squirrel":false,"events":["weekend"]}
1239+
console.log(JSON.parse(string).events);
1240+
// → ["weekend"]
1241+
```
1242+
11791243
## Summary
11801244

11811245
Objects and arrays (which are a specific kind of object) provide ways

0 commit comments

Comments
 (0)