Example 1 — json()
Parse JSON bodies.
JavaScript
const res = new Response('{"n":3}');
res.json().then((d) => console.log(d.n));How It Works
- Body must be valid JSON.
After fetch, inspect response.status / response.ok, then consume the body once with json() or text().
const res = new Response("hi", { status: 200 });
console.log(res.status, res.ok);
res.text().then(console.log);Parse JSON bodies.
const res = new Response('{"n":3}');
res.json().then((d) => console.log(d.n));Cannot read twice without clone.
const res = new Response("x");
res.text().then((t) => console.log(t));CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
6 people found this page helpful