I have a simple PostgreSQL function I want to pass complex JSON to:
CREATE OR REPLACE FUNCTION foo(sync_data json)
RETURNS json AS
$body$
DECLARE
...
END;
$body$ LANGUAGE plpgsql;
Calling it with something like this:
SELECT foo('{"deviceId": 1, "shops": [{"id": 1}, {"id": 2}]}'::json);
gives me the following error:
- ERROR: malformed array literal: "{"id": 1}"
- DETAIL: Unexpected array element.
- CONTEXT: PL/pgSQL function foo(json) line 8 at SQL statement
- SQL state: 22P02
I need to pass complex JSON as a parameter, containing arrays of objects.
Thanks!
PS: Here is the full function text:
CREATE OR REPLACE FUNCTION foo(sync_data json)
RETURNS json AS
$body$
DECLARE
res_json json;
device_id integer;
shops json ARRAY;
BEGIN
SELECT json_extract_path(sync_data, 'deviceId') INTO device_id;
SELECT json_array_elements(json_extract_path(sync_data, 'shops')) INTO shops;
SELECT json_build_object('devId', device_id) INTO res_json;
RETURN res_json;
END;
$body$ LANGUAGE plpgsql;