Manipulating Complex Objects I have a question about json

JSON is a format for writing text files (for structuring data and exchanging it over a network).

You can’t write JSON in a JS file (or any other language, not just JS). The syntax of JSON is based on JS objects, so that’s why it looks like the same thing. If you have a text file, and you call it example.json, and you write the contents of it like this:

{
    "artist": "Billy Joel",
    "title": "Piano Man",
    "release_year": 1973,
    "formats": [ 
      "CD",
      "8T",
      "LP"
    ],
    "gold": true
}

That’s valid JSON: you put everything in a { "key": "value" } structure, you have to use double quotes around strings, you are allowed to use true, false and numbers, and you can have lists of stuff (eg [1,2,3]). That’s it as well, you can’t put anything else in a JSON file.

It’s just a way of describing data in a simple way so that it’s easy to write programs to decode the data from the files, and save and encode data to to JSON (again, in any language, not just JS). You can pass those text files around, and they’re really easy to deal with.

The reason it’s given quite high priority in FCC stuff is that a lot of the time, with web stuff, what you’ll do is make a request to somewhere to get data, and what comes back is data in JSON format, which you then decode to a JS object (JSON.parse(someData)), and can then use in a JS program

1 Like