What exactly does response.json() do?

Lesson link:

The diagram in the lesson suggests that response.json() converts the response object to JSON but upon research, it seems that response.json() returns a promise that resolves to a JavaScript object. So which is it, i’m confused at this point

In a sense both are correct. JSON stands for Javascript Object Notation and at it’s barest description, converting to JSON just means getting a Javascript object. When you have a JSON file, this is just a file that contains a single stand-alone Javascript object, usually for use as data storage. Therefore when the Promise returns a Javascript object, that object is of course expressed in Javascript Object Notation i.e. it is a JSON.

Now when you’re being accurate a JSON isn’t specifically a Javascript object as it can be used as a data storage medium for many programming languages, however for your question I would say thinking of it as just a Javascript object should hopefully make it clear enough. Whilst response.json() doesn’t literally just do some direct conversion of a response to a JSON file or anything, that’s effectively what it is doing from the perspective of using the value retrieved from response.json() in your work.

1 Like

But then if the response is already a JavaScript object, what’s the point of that?

Which promise are you referring to? The one returned by fetch() or the one returned by response.JSON()?

The response is a JSON object. Slightly different things.

response.JSON() converts it into something you can use in JS.

1 Like

But the lesson diagram says the response gets converted to a JSON object

Is the JSON object a string version of the object?