2015-10-09 31 views
7

Aşağıdaki sorguda $ isComplete ve $ isValid bir dize olarak döndürülür. Ancak, boole değerleri olarak kaydedilirler. Bu alanların boole temsilini nasıl iade edebilirim?Postgresql'de, jsonb anahtarında dize yerine bir boolean değerini nasıl döndürebilirim?

query = 
    "SELECT 
     data #>> '{id}' AS id, 
     data #>> '{name}' AS name, 
     data #>> '{curator}' AS curator, 
     data #> '{$isValid}' as \"$isValid\", 
     data #> '{customer}' as customer, 
     data #> '{$createdTS}' as \"$createdTS\", 
     data #> '{$updatedTS}' as \"$updatedTS\", 
     data #> '{$isComplete}' as \"$isComplete\", 
     (count(keys))::numeric as \"numProducts\" 
    FROM 
     appointment_intakes, 
     LATERAL jsonb_object_keys(data #> '{products}') keys 
    GROUP BY id" 

cevap

10

Basitçe mantıksal bir metin döküm: boolean türü için geçerli değişmezleri hakkında

insert into jsonb_test values 
(3, '{"is_boolean" : "true"}'), 
(4, '{"is_boolean" : "false"}'), 
(5, '{"is_boolean" : "t"}'), 
(6, '{"is_boolean" : "f"}'), 
(7, '{"is_boolean" : "on"}'), 
(8, '{"is_boolean" : "off"}'); 

select id, data, (data->>'is_boolean')::boolean as is_boolean 
from jsonb_test 
where (data->>'is_boolean')::boolean 

id |   data   | is_boolean 
----+------------------------+------------ 
    1 | {"is_boolean": true} | t 
    3 | {"is_boolean": "true"} | t 
    5 | {"is_boolean": "t"} | t 
    7 | {"is_boolean": "on"} | t 
(4 rows) 

Oku: Ayrıca mantıksal değere diğer json metin değerlerini, örnekler yayınlayabileceğim

create table jsonb_test (id int, data jsonb); 
insert into jsonb_test values 
(1, '{"is_boolean" : true}'), 
(2, '{"is_boolean" : false}'); 

select id, data, (data->>'is_boolean')::boolean as is_boolean 
from jsonb_test 
where (data->>'is_boolean')::boolean 

id |   data   | is_boolean 
----+------------------------+------------ 
    1 | {"is_boolean": true} | t 
(1 row) 

Not in the documentation.

İlgili konular