I feel like I just need to skip something simple, but I went through the PostgreSQL documentation on JSON and JSON operators and functions and see nothing to explain.
Easy to turn things into JSON in PostgreSQL:
SELECT *, pg_typeof(j) FROM (VALUES (to_json(5)), (to_json(true)), (to_json('foo'::TEXT)) ) x (j);
will return me a good result, full json s:
j | pg_typeof -------+----------- 5 | json true | json "foo" | json
But how to convert these json values โโback to their original types? I do not expect that I can do this in one result set, of course, since the types are incompatible. I just mean individually.
Lots of things I've tried
Casting sure doesn't work:
SELECT to_json(5)::NUMERIC;
gives
ERROR: cannot cast type json to numeric
If I try to use the json_populate_record function as follows:
SELECT json_populate_record(null::INTEGER, to_json(5));
I get
ERROR: first argument of json_populate_record must be a row type
In PG 9.4, I can pretty easily specify a type: SELECT json_typeof(to_json(5)); gives number , but that doesn't help me extract it.
Also json_to_record (also 9.4):
SELECT * FROM json_to_record(to_json(5)) x (i INT);
gets another error:
ERROR: cannot call json_to_record on a scalar
So, how do you convert json "scalars" (since PG calls them apparently) to the corresponding type of PG?
I am interested in 9.3 and 9.4; 9.2 will be just a bonus.