Request.json()
The json()
method of the Request
interface reads the request body and returns it as a promise that resolves with the result of parsing the body text as JSON
.
Note that despite the method being named json()
, the result is not JSON but is instead the result of taking JSON as input and parsing it to produce a JavaScript object.
Syntax
request.json().then(data => { // do something with your data });
Parameters
None.
Return value
A Promise
that resolves to a JavaScript object. This object could be anything that can be represented by JSON — an object, an array, a string, a number...
Examples
const obj = {hello: 'world'}; const request = new Request('/myEndpoint', { method: 'POST', body: JSON.stringify(obj) }); request.json().then(function(data) { // do something with the data sent in the request });
Specifications
Specification |
---|
Fetch Standard (Fetch) # ref-for-dom-body-json① |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
json |
42 |
14 |
39 |
No |
29 |
10.1 |
42 |
42 |
39 |
29 |
10.3 |
4.0 |
See also
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Request/json