bhylu.com

Advertisements

JSON Fetch

A JSON response still needs HTTP and shape checks. This offline example models a response without contacting a server.

Where to run this

This example uses the JavaScript console playground. Select Run to execute it; the example does not run on this lesson page.

A practical example

JavaScript

const response = new Response('[{"title":"Repair club"}]');
const sessions = await response.json();
if (!Array.isArray(sessions)) throw new TypeError("Expected a list");
console.log(sessions.length);

What happens

The validated list contains one session.