準備報表輸入
本例只列沒有 note 鍵的文件。json_extract 對欠路徑及 JSON null 都回 SQL NULL;json_type 對明確 JSON null 回文字 “null”,欠路徑才 SQL NULL。此例全部 JSON 格式合法。
以下記錄及報表規則均為原創假設。在新 SQLite 測試資料庫重做建表,預覽包含此小型例子每張表。
| 編號 | 原 JSON 文字 |
|---|---|
| A | {"note":null} |
| B | {} |
| C | {"note":"ready"} |
CREATE TABLE documents(id TEXT PRIMARY KEY,payload TEXT NOT NULL CHECK(json_valid(payload))); INSERT INTO documents VALUES('A','{"note":null}'),('B','{}'),('C','{"note":"ready"}');比較錯誤及更正結果
SELECT id FROM documents WHERE json_extract(payload,'$.note') IS NULL ORDER BY id;| 編號 |
|---|
| A |
| B |
SELECT id FROM documents WHERE json_type(payload,'$.note') IS NULL ORDER BY id;| 編號 |
|---|
| B |
錯誤列 A、B,但只有 B 欠鍵。A 明確 null 留原資料,不當欠欄。
用新副本測邊界
note 有空字串也代表鍵存在,不入欠鍵列。
另開新資料庫重做原建表,才套下列改動及更正查詢。空結果在表內用已標明空結果文字表示,不是假資料行。
INSERT INTO documents VALUES('D','{"note":""}');
SELECT id FROM documents WHERE json_type(payload,'$.note') IS NULL ORDER BY id;| 編號 |
|---|
| B |
A 的 note:null 要列欠欄嗎?
查看參考答案
不用,鍵存在而值 JSON null;更正欠鍵列只有 B。