比较PostgreSQL 9.3 json对象中的布尔值

比较PostgreSQL 9.3 json对象中的布尔值,json,postgresql,postgresql-9.3,Json,Postgresql,Postgresql 9.3,有没有其他方法可以匹配PostgreSQL(Version9.3)json对象中的布尔值而不将其转换为字符串 我的意思是: 该表的jsoncolumn列中包含以下对象: '{"path":"mypath", "exists": true}' 以下查询获取记录(请注意,存在值以文本形式获取,并带有->): 而这一个没有: select * from thetable where jsoncolumn -> 'exists' = true; 我想知道是否有更合适的方法进行布尔比较?将值转换

有没有其他方法可以匹配PostgreSQL(Version9.3)json对象中的布尔值而不将其转换为字符串

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

'{"path":"mypath", "exists": true}'
以下查询获取记录(请注意,
存在
值以文本形式获取,并带有
->
):

而这一个没有:

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

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

将值转换为文本,然后转换为布尔值:

select pg_typeof((j ->> 'exists')::boolean)
from (values ('{"path":"mypath", "exists": true}'::json)) v(j)
;
 pg_typeof 
-----------
 boolean

以下是验证json(b)布尔值的所有有效组合:


你试过了吗:
(jsoncolumn->“exists”)::boolean=true
?@a_horse_,带有_no_name是的,我试过了。我的问题是如何查询该值是否为真,而不是检查它是否为布尔值。对不起,如果我的问题不清楚的话。那是为了说明如何获取布尔值而不是json。你可以按你的意图使用它,但这行不通。使用此方法无法区分
{“exists”:“true”}
{“exists”:true}
之间的区别。
select pg_typeof((j ->> 'exists')::boolean)
from (values ('{"path":"mypath", "exists": true}'::json)) v(j)
;
 pg_typeof 
-----------
 boolean
-- 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