json - 比较 PostgreSQL 9.3 json 对象中的 bool 值

标签 json postgresql postgresql-9.3

有没有其他方法可以匹配来自 PostgreSQL(9.3 版)json 对象的 bool 值而不将其转换为字符串?

我的意思是: 该表在其 jsoncolumn 列中包含以下对象:

'{"path":"mypath", "exists": true}'

以下查询获取记录(注意 exists 值是通过 ->>> 作为文本获取的):

select * from thetable where jsoncolumn ->> 'exists' = 'true';

而这个不是:

select * from thetable where jsoncolumn -> 'exists' = true;

我想知道是否有更合适的方法来进行 bool 比较?

最佳答案

这里是验证 json(b) bool 值的所有有效组合:

-- This works only with jsonb, not with json because in Postgres json type is just a string.
SELECT $${ "exists": true }$$::jsonb -> 'exists' = 'true';
-[ RECORD 1 ]
?column? | t

-- All the following works with regular json as well with jsonb:
SELECT ( $${ "exists": true }$$::json ->> 'exists' )::boolean;
-[ RECORD 1 ]
bool | t

SELECT ( $${ "exists": true }$$::json ->> 'exists' )::boolean IS TRUE;
-[ RECORD 1 ]
?column? | t

SELECT ( $${ "exists": true }$$::json ->> 'exists' )::boolean = TRUE;
-[ RECORD 1 ]
?column? | t

关于json - 比较 PostgreSQL 9.3 json 对象中的 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39493110/

相关文章:

json - 在 Go 中解码 json

postgresql - 通过redis-py将PostgreSQL时间戳存储为redis排序集的分数:DataError

dynamic - Postgres : How to get the value of a dynamic column from triggers

javascript - 如何更改 JSON 对象中对象的属性值

SQL JSON - 如何修改 Json 数据中存在的 bool 值

javascript - AngularJs : Restangular

java - PostgreSQL- getColumnName 不工作,返回别名

使用\i 命令从文件读取时 postgreSQL 权限被拒绝

postgresql - 为什么在索引扫描期间会读取这么多页面(Postgres 11.2)?

ruby-on-rails - 获取一个类别中所有帖子的最后评论