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).

72n5<3 sp ansty="le--0:#82AAFF;--1:#3B61">B0Respon5<3(sp ansty="le--0:#82AAFF;--1:#3B61">B0JSAB0stringi53(data)<);d ivclassNa="meco"><);d>72n5<3 sp ansty="le--0:#82AAFF;--1:#3B61">B0Respon5<3(sp ansty="le--0:#82AAFF;--1:#3B61">B0JSAB0stringi53({ message<: sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#ECC48D;--1:#984E">4D1<1"sp ansty="le--0:#D6DEEB;--1:#403F">53 })<);d ivclassNa="meco"><);d>72n5<3 sp ansty="le--0:#82AAFF;--1:#3B61">B0Respon5<3(sp ansty="le--0:#82AAFF;--1:#3B61">B0JSAB0stringi53(data), {}<);d ivclassNa="meco"><);d>72n5<3 sp ansty="le--0:#82AAFF;--1:#3B61">B0Respon5<3(sp ansty="le--0:#82AAFF;--1:#3B61">B0JSAB0stringi53(data),< {d ivclassNa="meec-li">53< sp ansty="le--0:#D6DEEB;--1:#403F">53headers: <{ sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#ECC48D;--1:#984E">4Dcontent-ty1<1"sp ansty="le--0:#D6DEEB;--1:#403F">53<: sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#ECC48D;--1:#984E">4Dapplication/js1<1"sp ansty="le--0:#D6DEEB;--1:#403F">53 <},d ivclassNa="meec-li">53}<);d ivclassNa="meco"><);d>
3F53Resp4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B03F53(da<;di vdivclass="Nameexpressive-">3F53Resp4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B03F53({ messa1<111" spans="tyle--0:#ECC48D;--1:#98">4E<4Dok spans="tyle--0:#D9F5DD;--1:#11">1<111" spans="tyle--0:#D6DEEB;--1:#40">3F53< }) ;divclass="Name">< })><;di vdivclass="Nameexpressive-">6E7<2new spans="tyle--0:#D6DEEB;--1:#40">361B0Resp361B04<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0strin3F53(da48483F<53, spans="tyle--0:#F78C6C;--1:#AA">0<9822 spans="tyle--0:#D6DEEB;--1:#40">3F5<3)) ;divclass="Name"><2))><;di vdivclass="Nameexpressive-">6E7<2new spans="tyle--0:#D6DEEB;--1:#40">361B0Resp361B04<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0strin3F53(data), { stat098<2404 spans="tyle--0:#D6DEEB;--1:#40">3F53< }) ;divclass="Name">< })><;di vdivclass="Nameexpressive-">6E7<2new spans="tyle--0:#D6DEEB;--1:#40">361B0Resp3F53(te

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.