Function to insert rows of json array into a table:
create table mytable(col1 text, col2 boolean, col3 boolean);
create function fun1(vja json[])
... as $$
begin
foreach v in array json_array_elements(vja)
loop
insert into mytable(col1, col2, col3)
values(v->'col1', v->'col2'::boolean, v->'col3'::boolean);
end loop;
end;
$$;
Call this function:
select fun1('[
{"col1": "turow1@af.com", "col2": false, "col3": true},
{"col1": "xy2@af.com", "col2": false, "col3": true}
]')
or this form:
select fun1('[
{"col1": "turow1@af.com", "col2": "false", "col3": "true"},
{"col1": "xy2@af.com", "col2": "false", "col3": "true"},
]')
or this form:
select fun1('[
{"col1": "turow1@af.com", "col2": "false", "col3": "true"},
{"col1": "xy2@af.com", "col2": "false", "col3": "true"},
]'::json[])
Always received:
ERROR: malformed array literal: "[ {"col1": "turow1@af.com", "col2": "false", "col3": "true"}, {"col1": "xy2@af.com", "col2": "false", "col3": "true"}, ]" LINE 2: '[ ^ DETAIL: "[" must introduce explicitly-specified array dimensions. SQL state: 22P02 Character: 136