尝试在postgresql中查询json文档时出现语法错误

时间:2017-07-16 23:11:28

标签: json database postgresql

在我的postgresql 9.6上,我遇到了一个像

这样创建的表的错误
create table jsonTest (id integer, data jsonb);

该表格包含以下数据:

select * from jsonTest;
 id |               data               
----+----------------------------------
  1 | {"tags": ["AA", "BB", "CC"]}
  2 | {"tags": ["BB", "DD"]}
  2 | {"tags": ["CC"]}
  4 | {"city": "austin", "tags": "EE"}
(4 rows)

但是当我尝试选择时,出现了语法错误:

template1=# select * from jsonTest where data->>city = 'austin';
ERROR:  column "city" does not exist
LINE 1: select * from jsonTest where data->>city = 'austin';

有什么想法吗?非常感谢!

1 个答案:

答案 0 :(得分:2)

JSON对象的->>运算符的{p> The right operand text。使用撇号作为键,因为没有它们Postgres将名称解释为标识符(列名):

data->>'city'