6
任何人都可以看到爲什麼這不起作用嗎?根據手冊的section 9.15,->
運算符應該訪問JSON數據類型的元素。它看起來對我來說,雖然信息架構表示,列的類型是「JSON」它仍然是一個真正的標串如何在postgresql中的JSON類型列上使用JSON運算符9.3
postgres=# create table jtest (id serial, data json);
CREATE TABLE
postgres=# select column_name, data_type from information_schema.columns where table_name = 'jtest';
column_name | data_type
-------------+-----------
id | integer
data | json
(2 rows)
postgres=# insert into jtest (data) values (to_json('{"k1": 1, "k2": "two"}'::text));
INSERT 0 1
postgres=# select * from jtest;
id | data
----+--------------------------------
1 | "{\"k1\": 1, \"k2\": \"two\"}"
(1 row)
postgres=# select data->'k1' from jtest;
ERROR: cannot extract element from a scalar
postgres=# select data::json->'k1' from jtest;
ERROR: cannot extract element from a scalar
postgres=# \q
$ pg_ctl --version
pg_ctl (PostgreSQL) 9.3beta2
更新(注意引號顯示時):
我發現這兩個帖子here和here表明它應該像我一樣工作。只是要確定我嘗試這樣做:
postgres=# select * from jtest where data ->> 'k2' = 'two';
ERROR: cannot extract element from a scalar
是否有構建選項或contrib請模塊我需要得到JSON的功能?
謝謝你的職位;也有使用to_json轉換成json列的相同問題。我回去並使用CAST(field_name作爲json),一切都按預期工作。 –