How do you distinguish an absent JSON field from explicit null?

json_type returns SQL NULL for a missing path and the text null for an explicitly null JSON value.

SQLite example and interpretation

An omitted age field and an explicitly empty answer carry different meanings. Looking at the extracted scalar alone collapses both to SQL NULL.

SELECT json_type('{}','$.age') IS NULL,json_type('{"age":null}','$.age');

Result [[1, "null"]]

中文

如何区分 JSON 中缺失字段和显式 null?

json_type 在路径缺失时返回 SQL NULL,在路径指向 JSON null 时返回文本 null。

SQLite 示例与适用条件

问卷未含年龄字段与主动留空代表不同状态。只看 json_extract 的值,这两种情形都会得到 SQL NULL。

SELECT json_type('{}','$.age') IS NULL,json_type('{"age":null}','$.age');

结果 [[1, "null"]]

References / 参考来源

Keep reading / 继续阅读