I'm trying to use Postgres (PL/pgSQL) to communicate with a shell script that expects JSON data. A simplified example of what I'm trying to do is
CREATE OR REPLACE FUNCTION json_func(IN json_in JSONB, OUT json_out JSONB)LANGUAGE plpgsqlAS $code_block$BEGINCREATE TEMPORARY TABLE tmp (json_tmp JSONB);EXECUTE format($bash$ COPY tmp FROM PROGRAM 'echo ''%s'' | jq . ' $bash$, json_in);SELECT json_tmp FROM tmp INTO json_out;DROP TABLE tmp;END;$code_block$;
When I run
SELECT json_func('{"key1": "val1", "key2": "val2"}'::JSONB);
I get
ERROR: invalid input syntax for type jsonDETAIL: The input string ended unexpectedly.CONTEXT: JSON data, line 1: {COPY tmp, line 1, column json_tmp: "{"SQL statement " COPY tmp FROM PROGRAM 'echo ''{"key1": "val1", "key2": "val2"}'' | jq . '"PL/pgSQL function json_func(jsonb) line 4 at EXECUTE
I've tried various ways of escaping the single quotes needed to surround the JSON input, but no luck.