# JSON

JSON (pronounced "Jason") is a widely used data storage and communication format for the web, and can be used to share data between applications, or the server and the client. It resembles how we write objects, but with a few restrictions. All property names must be surrounded by double quotes, and only simple data expressions are allowed (no functions calls, variables, or anything involving actual computation, like JavaScript objects can contain). Comments are also not allowed.

A JSON can be written as:

```
[
    ,
    {
        "name" : "Bruce Wayne",
        "superhero" : "Batman",
        "year_borne" : 1939,
        "hobby" : "Being rich"
    }
]
```

Note the comma between each key-value pair, and that there is no comma following the last such pair. In the example, there are two objects, stored in a list, which is normal when storing several objects in a JSON file.

JSON files have the ".json" extension.

Included in JavaScript are the two functions `JSON.stringify()` and `JSON.parse()` that convert data from and to JSON. The first one takes a JavaScript value and returns a JSON-encoded string. The other takes such a string and converts it to the value it encodes.

```
const spiderman = {
        "name" : "Peter Parker",
        "superhero" : "Spider-Man",
        "year\_borne" : 1962,
        "hobby" : "Photography"
    }

JSON.stringify(spiderman)

>> "{"name":"Peter Parker","superhero":"Spider-Man","year_borne":1962,"hobby":"Photography"}"

// Store the Spider-Man object as JSON
const spiderJSON = JSON.stringify(spiderman)

// Print out Spider-Man's hobby by converting the JSON to a JavaScript Object
console.log(JSON.parse(spiderJSON).hobby)

>> "Photography"
```
