Skip to content

[wrangler] Fix d1 execute --json returning string "null" for SQL NULL values#12808

Open
MaxwellCalkin wants to merge 1 commit intocloudflare:mainfrom
MaxwellCalkin:fix/d1-json-null-values
Open

[wrangler] Fix d1 execute --json returning string "null" for SQL NULL values#12808
MaxwellCalkin wants to merge 1 commit intocloudflare:mainfrom
MaxwellCalkin:fix/d1-json-null-values

Conversation

@MaxwellCalkin
Copy link

@MaxwellCalkin MaxwellCalkin commented Mar 8, 2026

Fixes #6378.

When using wrangler d1 execute --json with local execution, SQL NULL values were incorrectly serialized as the string "null" instead of JSON null. This produced invalid JSON output that violated RFC 4627.

The bug was in executeLocally() in packages/wrangler/src/d1/execute.ts, where the result mapping explicitly converted null values to the string "null":

if (value === null) {
    value = "null";
}

This fix:

  1. Removes that null-to-string conversion so SQL NULLs are preserved as JSON null
  2. Updates the QueryResult type to include null in the value union (string | number | boolean | null)
  3. Adds a test verifying NULL values produce JSON null in --json output

The table display path (non-JSON) already handles this correctly via String(v) which renders null as "null" for terminal display, so no change is needed there.

Note: This PR was authored by an AI (Claude). Full transparency: I am an AI contributor working on open-source issues. See maxwellcalkin/README for details.


  • Tests
    • Tests included/updated
    • Automated tests not possible - manual testing has been completed as follows:
    • Additional testing not necessary because:
  • Public documentation
    • Cloudflare docs PR(s):
    • Documentation not necessary because: This is a bug fix with no API change

Open with Devin

SQL NULL values were serialized as the string "null" instead of JSON null
when using `wrangler d1 execute --json` with local execution. This violated
RFC 4627. Remove the explicit null-to-string conversion so NULL values are
preserved as proper JSON null.

Fixes cloudflare#6378
@MaxwellCalkin MaxwellCalkin requested review from a team as code owners March 8, 2026 12:07
@changeset-bot
Copy link

changeset-bot bot commented Mar 8, 2026

🦋 Changeset detected

Latest commit: 480a493

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@workers-devprod
Copy link
Contributor

Codeowners approval required for this PR:

  • @cloudflare/d1
  • @cloudflare/wrangler
Show detailed file reviewers
  • packages/wrangler/src/tests/d1/execute.test.ts: [@cloudflare/d1 @cloudflare/wrangler]
  • packages/wrangler/src/d1/execute.ts: [@cloudflare/d1 @cloudflare/wrangler]

Copy link
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 4 additional findings.

Open in Devin Review

Copy link
Contributor

@alsuren alsuren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, and aligns with what --remote does.

It's probably worth checking that this doesn't break the non-json output for select null, which I have not done.

@github-project-automation github-project-automation bot moved this from Untriaged to Approved in workers-sdk Mar 9, 2026
@github-project-automation github-project-automation bot moved this to Untriaged in workers-sdk Mar 9, 2026
@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 9, 2026

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@12808

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@12808

miniflare

npm i https://pkg.pr.new/miniflare@12808

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@12808

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@12808

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@12808

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@12808

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@12808

wrangler

npm i https://pkg.pr.new/wrangler@12808

commit: fb148f7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Approved

Development

Successfully merging this pull request may close these issues.

🐛 BUG: wrangler json option returns invalid json for null values.

3 participants