Globals
Response.json()
Consumes and parses the response body as JSON. It rejects for malformed JSON or a consumed body; successful parsing is not schema validation. Check status and limit response size in production before allocating or trusting data. Tested on Node.js 22.16.0. Example output (one line per entry): ["true 4"].
Formal Syntax & Structure
nodejs syntaxnodejs
response.json()Return Value
Promise<any>
Parsed JavaScript value.
Working Example
Example Snippetnodejs
import http from 'node:http';const server = http.createServer((q, r) => { r.setHeader('Content-Type', 'application/json'); r.end(JSON.stringify({ stock: 4 })); });await new Promise(r => server.listen(0, '127.0.0.1', r));const url = 'http://127.0.0.1:' + server.address().port;try { const r = await fetch(url); const value = await r.json(); console.log(Number.isInteger(value.stock), value.stock);}finally { server.closeAllConnections(); await new Promise(r => server.close(r));}