JavaScript Asynchronous Programming with JavaScript Understanding Promises Using Fetch

Farid Lavizadeh
Farid Lavizadeh
11,611 Points

Don't understand json() method.

Data in URL is already in JSON format. We used to parse it so it would convert to an object. Why are we using json() method to parse it back to JSON now? And if it is parsed to JSON how is being used as an Object?

1 Answer

Rolando Bernal
Rolando Bernal
18,105 Points

The way I understand it is that whenever we receive data from a web server, the data is always a string. So we need to parse the data with JSON.parse() so the data becomes a JavaScript object. This object could be anything that can be represented by JSON — an object, an array, a string, a number... Once we have the data as a JS object, then we can do whatever we need with it.

Farid Lavizadeh
Farid Lavizadeh
11,611 Points

I understand that, but what is the difference between the following two methods; .json() for JSON.parse()? Does .json() convert a promise object to a regular object? Whereas JSON.parse converts JSON to a regular Object?