--Takes a json array to parse each elements as text .But it shows an error. --Change the type from json[] to json but not helps.
CREATE OR REPLACE FUNCTION public.test(in_param json[])
RETURNS integer
LANGUAGE 'plpgsql'
COST 100
VOLATILE
AS $BODY$
declare
item text;
arr1 text[];
begin
for item in select * from json_array_elements_text(in_param ) loop
arr1 = arr1||item;
end loop;
return 0;
end;
$BODY$;
ALTER FUNCTION public.test(json[])
OWNER TO postgres;
I think you're mistaken. ["a","b","c"]
cannot be cast to json[]
. Appending square brackets ([]
) creates a Postgres array datatype and is not self castable to a JSON
array type
You'll receive this if you did so.
ERROR: malformed array literal
Rather, define your function to accept a simple JSON type
create or replace function test(json_in json) ..
..
Now, simply quoting your sample argument ["a","b","c"]
will work
select test('["a","b","c"]')
your are trying to pass an array try the following
String t[] = { "a", "b" };
JSONArray mJSONArray = new JSONArray(Arrays.asList(mStringArray));
test(mJSONArray)