Response.json
Returns a new Response object with a JSON-encoded body and the Content-Type header set to application/json
Syntax
Response.json(data, init?)Parameters
| Parameter | Type | Description |
|---|---|---|
| data | any | The data to JSON-encode as the response body |
| init | ResponseInit | Optional response init (status, headers) |
Return Value
A new Response object with JSON body
Examples
const response = Response.json({ message: 'Hello' })
const data = await response.json()
console.log(data.message) // 'Hello'const errorResponse = Response.json(
{ error: 'Not found' },
{ status: 404 }
)
console.log(errorResponse.status) // 404function apiResponse<T>(data: T, status = 200) {
return Response.json(data, { status })
}
const res = apiResponse({ users: ['Alice', 'Bob'] })
console.log(await res.json())Understanding Response.json
The Response.json method in JavaScript returns a new Response object with a JSON-encoded body and the Content-Type header set to application/json. It belongs to the Response object and is one of the most widely used methods for working with response values in modern JavaScript and TypeScript applications.
The method signature is Response.json(data, init?). It accepts 2 parameters: data, init. When called, it returns a new response object with json body. Understanding when and how to use json() helps you write more expressive, readable code.
Common use cases for Response.json include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like response-constructor, fetch-response-json, request-constructor, enabling you to chain operations together for complex data manipulation pipelines.
Supported in Chrome 105+, Firefox 115+, Safari 17+, Edge 105+, and Node.js 20+.
Browser Compatibility
Supported in Chrome 105+, Firefox 115+, Safari 17+, Edge 105+, and Node.js 20+.
Related Methods
More Response Methods
Other methods in the Response object
Related Tools
More Response Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.