{"record":{"id":"0cacd85bfae6438b","repo":"oxc-project/oxc","slug":"prefer-using-response-json-over-json-stringi","errorCode":null,"errorMessage":"Prefer using `Response.json(…)` over `JSON.stringify()`.","messagePattern":"Prefer using `Response\\.json\\(…\\)` over `JSON\\.stringify\\(\\)`\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_response_static_json.rs","lineNumber":17,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{CallExpression, Expression, NewExpression},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\n\nuse crate::{\n    AstNode,\n    ast_util::{could_be_asi_hazard, is_method_call, is_new_expression},\n    context::LintContext,\n    rule::Rule,\n};\n\nfn prefer_response_static_json_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Prefer using `Response.json(…)` over `JSON.stringify()`.\").with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferResponseStaticJson;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Enforces the use of `Response.json()` over `new Response(JSON.stringify())`.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// `Response.json()` is a more concise and semantically clear way to create JSON responses.\n    /// It automatically sets the correct `Content-Type` header (`application/json`) and handles\n    /// serialization, making the code more maintainable and less error-prone.\n    ///\n    /// ### Examples\n    ///","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_response_static_json.rs#L1-L35","documentation":"This is the oxlint rule `unicorn/prefer-response-static-json` firing. It flags `new Response(JSON.stringify(...))` because the Fetch API provides a static `Response.json(data)` factory that serializes the body and sets the correct `Content-Type: application/json` header for you. The rule is emitted as a style warning by the `unicorn` plugin inside oxc's linter.","triggerScenarios":"A `new Response(...)` expression (or `Response` constructor call) whose single argument is a `JSON.stringify()` call, e.g. in a service worker, Cloudflare Worker, or fetch handler returning `new Response(JSON.stringify(data))`. The rule matches via `is_new_expression` on `Response` combined with a `JSON.stringify` argument.","commonSituations":"Writing or reviewing fetch handlers / edge functions that hand-serialize payloads with `JSON.stringify` and manually set a JSON content-type header; migrating an ESLint `unicorn` config to oxlint and seeing the rule activate for the first time.","solutions":["Replace `new Response(JSON.stringify(data), { headers: { 'content-type': 'application/json' } })` with `Response.json(data)`.","If you support runtimes without `Response.json` (older Node < 18.11 / old service-worker scopes), disable the rule: `\"unicorn/prefer-response-static-json\": \"off\"` in `.oxlintrc.json`.","If you intentionally need custom serialization (replacer/space args), keep `JSON.stringify` and add an eslint-disable/oxlint-disable comment for that line."],"exampleFix":"// before\nreturn new Response(JSON.stringify({ ok: true }), {\n  headers: { 'content-type': 'application/json' },\n});\n\n// after\nreturn Response.json({ ok: true });","handlingStrategy":"validation","validationCode":"// CI gate: fail the build on the rule before merge\n// package.json script: \"lint\": \"oxlint --deny-warn unicorn/prefer-response-static-json src/\"\nimport { execSync } from 'node:child_process';\nexecSync('npx oxlint src/', { stdio: 'inherit' });","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer the static factories `Response.json(data)` / `Response.error()` / `Response.redirect()` whenever you construct JSON responses.","Configure oxlint in CI (`oxlint --deny-warn`) so style regressions surface in the PR, not production review.","Check runtime support (Node 18.11+/modern browsers) for Response.json before standardizing on it."],"tags":["oxlint","unicorn","fetch-api","style","response"],"backgroundTag":"modernize-web-api-usage","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}