我見過很多與此類似的問題(例如this one),但找不到有幫助的問題。我正在尋找 Javascript 的等價物,JSON.stringify()但對于 PostgreSQL。
例如:
db=# \pset null
Null display is "(null)".
db=# create table x(c1 json);
CREATE TABLE
db=# insert into x (c1) values (null), ('"blah"'::json), ('{"a":1}'::json);
INSERT 0 3
db=# select * from x;
c1
---------
(null)
"blah"
{"a":1}
(3 rows)
db=# select '''' || c1::text || '''' from x;
?column?
-----------
(null) --- this is wrong, should be 'null'
'"blah"'
'{"a":1}'
(3 rows)
db=# select '''' || (c1 #>> '{}') || '''' from x;
?column?
-----------
(null) --- this is wrong, should be 'null'
'blah' --- this is wrong, should be '"blah"'
'{"a":1}'
(3 rows)
db=# select '''' || case when c1 is null then 'null' else c1::text end || '''' from x;
?column?
-----------
'null'
'"blah"'
'{"a":1}'
(3 rows)
最后一個例子可以不用CASE...END子句來完成嗎?對于所有可能的輸入,它總是正確的嗎?
uj5u.com熱心網友回復:
您將 JSON null 與 SQL NULL 混淆了。每這里JSON 型別:
表 8.23。JSON 基本型別和對應的 PostgreSQL 型別
JSON 原始型別 PostgreSQL 型別 注釋
...
null (none) SQL NULL 是一個不同的概念
所以:
insert into x (c1) values ('null'), ('"blah"'::json), ('{"a":1}'::json);
select '''' || c1::text || '''' from x;
?column?
-----------
'null'
'"blah"'
'{"a":1}'
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/467320.html
標籤:json PostgreSQL
