Skip to content

Commit 6ed249b

Browse files
[wrangler] Fix d1 execute --json returning string "null" for SQL NULL values (#12808)
1 parent e4d9510 commit 6ed249b

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Fix `wrangler d1 execute --json` returning `"null"` (string) instead of `null` (JSON null) for SQL NULL values
6+
7+
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 fix removes the explicit null-to-string conversion so NULL values are preserved as proper JSON null in the output.

packages/wrangler/src/__tests__/d1/execute.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,23 @@ describe("execute", () => {
230230
]);
231231
});
232232

233+
it("should output JSON null for SQL NULL values with --json flag", async ({
234+
expect,
235+
}) => {
236+
setIsTTY(false);
237+
writeWranglerConfig({
238+
d1_databases: [
239+
{ binding: "DATABASE", database_name: "db", database_id: "xxxx" },
240+
],
241+
});
242+
243+
await runWrangler(
244+
"d1 execute db --command 'SELECT 1 as id, null as name;' --json"
245+
);
246+
const parsed = JSON.parse(std.out);
247+
expect(parsed[0].results[0].name).toBeNull();
248+
});
249+
233250
describe("duration formatting", () => {
234251
mockAccountId({ accountId: "some-account-id" });
235252
mockApiToken();

packages/wrangler/src/d1/execute.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import type { D1Result } from "@cloudflare/workers-types/experimental";
3333
import type { ComplianceConfig, Config } from "@cloudflare/workers-utils";
3434

3535
export type QueryResult = {
36-
results: Record<string, string | number | boolean>[];
36+
results: Record<string, string | number | boolean | null>[];
3737
success: boolean;
3838
meta?: {
3939
duration?: number;
@@ -328,9 +328,6 @@ async function executeLocally({
328328
if (Array.isArray(value)) {
329329
value = `[${value.join(", ")}]`;
330330
}
331-
if (value === null) {
332-
value = "null";
333-
}
334331
return [key, value];
335332
})
336333
)

0 commit comments

Comments
 (0)