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 Response(JSON.stringify(data));new Response(JSON.stringify({ message: "ok" }));new Response(JSON.stringify(data), {});new Response(JSON.stringify(data), { headers: { "content-type": "application/json" },});Response.json(data);Response.json({ message: "ok" });new Response(JSON.stringify(data, null, 2));new Response(JSON.stringify(data), { status: 404 });new Response(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
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.