I'm trying to query a table with a JSON column which will always hold an array of "primitive" values (i.e. integers, strings, booleans -- not objects or arrays).
My query should be similar to [ref2], but I can't do ->>'id'
because I'm not trying to access a JSON object but the value itself.
In the [ref1] fiddle (blatant fork from the above), there's and incomplete query... I'd like to query all things
which contain 3
among its values.
Even more so, I'd like some rows to have arrays of strings, other rows to have arrays of integers, and other ones arrays of booleans... So casting is undesiderable.
I believe ->>
returns the original JSON value type, but I need the "root" object... That is, my JSON value is [1,2,3,4]
, using json_array_elements
should yield e.g. 2
, but that is a JSON type according to my tests.
Upgrading to 9.4 is planned in the near future, but I haven't read anything yet that gave me a clue jsonb
would help me.
UPDATE: at the moment, I'm (1) making sure all values are integers (mapping non-integers values to integers), which is suboptimal; (2) querying like this:
SELECT *
FROM things, json_array_elements(things.values) AS vals
WHERE vals.value::text::integer IN (1,2,3);
I need the double casting (otherwise it complains that cannot cast type json to integer
).