From the course: JavaScript: Ajax and Fetch

Unlock this course with a free trial

Join today to access over 25,600 courses taught by industry experts.

Work with JSON data

Work with JSON data

After you receive the response from your Ajax request, you have to dig into it and find the specific data you need. JavaScript Object Notation, or JSON, is by far the most common format used to exchange data on the modern web. JSON data is transmitted as a string, so in general, the first thing you need to do is parse it into the data format it encodes. Although the word object is in the name, JSON data could parse out as either an object or an array, which is technically just another type of object in JavaScript. To parse a JSON string, you use the parse method of the JSON object, which is built into JavaScript. However, the basic structure of a fetch request takes care of the parsing for you, so you don't actually need to use the JSON.parse command. In my create request function, I pass the API response to my handle errors function, and the first line of that function passes the data from a successful response through the response JSON method, which returns an object or an array. So…

Contents