Skip to content

responseJsonMethods

Prefer Response.json() over new 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).

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" },
});

This rule is not configurable.

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.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.