Manipulating Complex Objects I have a question about json

Tell us what’s happening:

I wonder what is the main difference between Object and Json and if my Object has the keys enclosed in double quotes, will it become a Json ?

Your code so far


var myMusic = {
    "artist": "Billy Joel",
    "title": "Piano Man",
    "release_year": 1973,
    "formats": [ 
      "CD",
      "8T",
      "LP"
    ],
    "gold": true
  }
 
**Your browser information:**

User Agent is: <code>Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) coc_coc_browser/77.0.126 Chrome/71.0.3578.126 Safari/537.36</code>.

**Link to the challenge:**
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/manipulating-complex-objects

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

Thanks for the reply but I still wonder about this, for example I have 2 Objects as follows

var firstObj = { name: “Bill” }

var secondObj = { “name”: “Bean”}

So is there any difference between two ways to write key like that?

Object keys are always strings in JS, but you can leave off the quotation marks if it’s a valid identifier (ie anything you could use as a variable name) — so if the key was “example”, you can do

{
  example: 1
}

But if it wasn’t a valid identifier, like this-is-an-example, you’d need to put quote marks around it:

{
  "this-is-an-example": 1
}

Note none of the keys in the challenge code need to be in quotation marks, they’re there to make it look like a standard JSON response you’d get back if your program had asked a server to give you a list of albums.

2 Likes

Interesting, I have yet to find an api that format the data as JSON, most look like regular objects, like this api https://jsonplaceholder.typicode.com/posts

Makes me wonder if formatting data as JSON is an old thing?, for example using JSON.stringify() if I were to make a post request to the jsonplaceholder api.

That’s probably because you have some browser extension that prettifies JSON for you. The actual response is JSON.

Oh, yeah I do, well that makes sense, but should I still use JSON.stringify() to format the data? I never have and it still works.

I guess if you use fetch to post data or want to store data in localStorage you have to stringify.

1 Like

I’m not sure I understand, that is a JSON response, that’s what JSON looks like. As @jenovs says, prettified so that it’s easier to read, but that’s a JSON response

From your message (and other documentations) a JSON response looks like this {"key:" "value"} so both the key and value within “”, but as the other user said it’s because I’m using the Chrome extension I’ve always seen the response as a regular object {key: "value"}

Yes, for strings. But you can have numbers or booleans (true/false) — logically, you have to be able to have those because otherwise what is "true" or "1", are they strings or not? It would be impossible to pass numbers around, which would make it useless. You can use strings, true/false, numbers, and you can have lists of stuff and collections like {“key”: “value”}

Can someone explain why my code isn’t working? I can’t figure it out.

var myMusic = [
{
“artist”: “Billy Joel”,
“title”: “Piano Man”,
“release_year”: 1973,
“formats”: [
“CD”,
“8T”,
“LP”
],
“gold”: true
}
// Add record here
{
“artist”: “U2”,
“title”: “Joshua Tree”,
“release_year”: 1985,
“formats”: [
“CD”,
“8T”,
“LP”
],
}
];

If you have a new question, please post it in a new thread instead of bumping an old one. Thanks :slight_smile: