Trabla: Postgresql: ERROR: cannot call json_array_elements on a scalar SQL state: 22023
Solving:
This error appears when json is "null" string
SELECT json_array_elements('null'::json)
ERROR: cannot call json_array_elements on a scalar
********** Error **********
ERROR: cannot call json_array_elements on a scalar
SQL state: 22023
and works fine when correct json array
SELECT json_array_elements('["a","b","c"]'::json)
Output:
json_array_element json
------------------------------
""a""
""b""
""c""
To fix use 'IS NULL' check with CASE
SELECT
CASE
WHEN 'null'::json IS NULL
THEN '[]'
END
OR
use replace function with type casting
SELECT
json_array_elements(
replace(
('null'::json)::character varying,
'null',
'[]'
)::json
)
No comments:
Post a Comment