responseJsonMethods
Prefer
Response.json()overnew Response(JSON.stringify(...))for JSON responses.
✅ This rule is included in the ts stylistic presets.
The Response.json() static method provides a cleaner way to create JSON responses compared to manually calling new Response(JSON.stringify(data)).
Using Response.json() is more concise and automatically sets the Content-Type header to application/json.
This rule flags cases where new Response(JSON.stringify(data)) can be replaced with Response.json(data).
Examples
Section titled “Examples”new var Response: new (body?: BodyInit, init?: ResponseInit) => Response
Response(var JSON: JSON
An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON.JSON.stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string (+1 overload)
Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
stringify(const data: any
data));new var Response: new (body?: BodyInit, init?: ResponseInit) => Response
Response(var JSON: JSON
An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON.JSON.stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string (+1 overload)
Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
stringify({ message: string
message: "ok" }));new var Response: new (body?: BodyInit, init?: ResponseInit) => Response
Response(var JSON: JSON
An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON.JSON.stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string (+1 overload)
Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
stringify(const data: any
data), {});new var Response: new (body?: BodyInit, init?: ResponseInit) => Response
Response(var JSON: JSON
An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON.JSON.stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string (+1 overload)
Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
stringify(const data: any
data), { ResponseInit.headers?: HeadersInit
headers: { "content-type": "application/json" },});var Response: typeof Response
Response.Response.json(data: any, init?: ResponseInit): Response
json(const data: any
data);var Response: typeof Response
Response.Response.json(data: any, init?: ResponseInit): Response
json({ message: string
message: "ok" });new var Response: new (body?: BodyInit, init?: ResponseInit) => Response
Response(var JSON: JSON
An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON.JSON.stringify(value: any, replacer?: (number | string)[] | null, space?: string | number): string (+1 overload)
Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
stringify(const data: any
data, null, 2));new var Response: new (body?: BodyInit, init?: ResponseInit) => Response
Response(var JSON: JSON
An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON.JSON.stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string (+1 overload)
Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
stringify(const data: any
data), { ResponseInit.status?: number
status: 404 });new var Response: new (body?: BodyInit, init?: ResponseInit) => Response
Response(const text: any
text);Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you need to pass additional options to JSON.stringify() like a replacer or space parameter, you’ll need to use the manual approach.
Similarly, if you need to set custom response options like status or additional headers beyond Content-Type, use new Response() directly.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
useStaticresponseJsonMethods