I am trying to write a function (f_all) that basically just calls other "partial" functions (e.g. f_1,f_2,f_3) and merges the results of them into one final JSON object. Function f_all will take a string array (e.g '1,2,3') and for every number in the array, it will add corresponding key in the resulting JSON , e.g. "part1" and value, which will be a result of the "partial" function.
So for example, for f_all('1,2,3') it should return JSON:
{"part1": {<result of f_1>},
 "part2": {<result of f_2>},
 "part3": {<result of f_3>}
}
I managed to achieve something similar using a FOR loop to append the result of each "partial" function to resulting array with array_append(). However, I need the result to be a simple JSON/JSONB object, not an array of objects.
This works and returns an array of json objects:
CREATE OR REPLACE FUNCTION f_all(ingeom geometry, parts text)
 RETURNS jsonb []
 LANGUAGE plpgsql
AS $function$
DECLARE 
    arr TEXT ARRAY;
    i text;
    json_partx jsonb;
    resultjson_part jsonb;
    resultjson jsonb;
    resultarray json [];
 BEGIN
    arr = string_to_array(parts, ',');
    
    FOREACH i in ARRAY arr
    LOOP
        EXECUTE 'SELECT f_' || i || '($1)' into json_partx using ingeom;
        resultjson_part = json_build_object('part' || i, json_partx);
        resultjson =  array_append(resultjson, resultjson_part);
    END LOOP;
        
    RETURN (resultjson);
END;
$function$;
In a similar manner, I want, in each loop iteration, to append key and value pairs into JSON object and return the resulting object.
 
    