I would like to do some operations with numbers stored in Postgresql 9.4 JSON
format. Suppose I have this table:
CREATE TABLE t (id SERIAL, a JSON);
INSERT INTO t (a)
VALUES ('[[0.1, 2.0], [0.3, 4.0], [0.5, 6.0]]'),
('[[0.7, 8.0], [0.9, 10.0]]');
So I can decompose that with e.g. json_array_elements()
:
SELECT id, json_array_elements(a) AS sub1 FROM t;
id | sub1
----------------
1 | [0.1, 2.0]
1 | [0.3, 4.0]
1 | [0.5, 6.0]
2 | [0.7, 8.0]
2 | [0.9, 10.0]
Note, that the number of elements may vary. Now I would like to do some calculations with the sub-elements, like:
SELECT id,
((json_array_elements(a)->>0)::FLOAT) *
((json_array_elements(a)->>1)::FLOAT) as result
FROM t
However I get the error: ERROR: functions and operators can take at most one set argument
. Any hints?