Search Postgresql Archives

Re: Checking if a json-typed column contains a key

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> With the hstore you can do hstore ? 'key' to check if the object contains
> the key-- is there a similar function for json objects?

Is this good enough?:

=> select ('{"a":1,"b":null}'::json) -> 'a';
 ?column?
----------
 1

=> select ('{"a":1,"b":null}'::json) -> 'b';
 ?column?
----------
 null

=> select ('{"a":1,"b":null}'::json) -> 'c';
 ?column?
----------
 NULL

=> select (('{"a":1,"b":null}'::json) -> 'a') IS NULL;
 ?column?
----------
 f

Time: 0.334 ms
=> select (('{"a":1,"b":null}'::json) -> 'b') IS NULL;
 ?column?
----------
 f

=> select (('{"a":1,"b":null}'::json) -> 'c') IS NULL;
 ?column?
----------
 t

If you want to treat {"b":null} and absence of "b" the same way you
can use ->> instead of ->.

This does seem worrisome though:

=> select (('{"a":1, "b":null}'::json) -> 'b')::text;
 text
------
 null

I think json_typeof solves that in 9.4, but I don't see anything in
9.3 other than ->>.

Paul


-- 
_________________________________
Pulchritudo splendor veritatis.


-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general




[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux