I'm encountering an issue when attempting to append a JSON array or object into another JSON array within a PostgreSQL function. It seems that the array_append function is inserting the JSON as a string, resulting in an unexpected format in the output.
Currently, I'm getting output like this:
{"{"category_id":8,"category_name":"08 Candy","is_active":true,"category_name_app":"Candy","display_order":7}"} However, I'd like the output to be in this format so that I can easily decode it in my code:
[{"category_id":8,"category_name":"08 Candy","is_active":true,"category_name_app":"Candy","display_order":7}]
Below is the logic of my function:
for all_categories in select * from categories where is_active = '1' loop
show_at_homepage = 0;
for current_subcat in select * from public."V_category_to_sub_category_w_names" where category_id = all_categories.category_id and sub_category_is_active = '1' loop
select * into product_count from public."V_APP_products_w_sub_categories" where sub_category_id = current_subcat.sub_category_id and store_id = get_store_id and is_deleted='0';
if count(product_count) > 0 then
show_at_homepage = 1;
end if;
end loop;
if show_at_homepage = 1 then
select row_to_json(all_categories) into cat_json;
select array_append(my_json_result_array,cat_json) into my_json_result_array;
end if;
end loop;
return my_json_result_array;