postgresql: mapping json array to string array I have following PostgreSql function:
CREATE OR REPLACE FUNCTION &quot;fn_get_list_invoice_number_by_product_ids&quot;(_product_ids INT[])



RETURNS &quot;pg_catalog&quot;.&quot;json&quot; AS $BODY$
    
BEGIN   
    RETURN (SELECT json_agg((record)) FROM 
                        (
                                SELECT DISTINCT invoice_number FROM sale.invoice_item 
                                WHERE product_id = ANY(_product_ids)
                        ) record
                    );
END;$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

After using this function I get following result:
[{&quot;invoice_number&quot;:&quot;POS20-000043A&quot;}, 
 {&quot;invoice_number&quot;:&quot;POS20-000002E&quot;}, 
 {&quot;invoice_number&quot;:&quot;POS20-000009E&quot;}, 
 {&quot;invoice_number&quot;:&quot;POS20-000003E&quot;}, 
 {&quot;invoice_number&quot;:&quot;POS20-000046A&quot;}]

But if I want result as below, what should I do?
[
      &quot;POS20-000043A&quot;,
      &quot;POS20-000002E&quot;,
      &quot;POS20-000009E&quot;,
      &quot;POS20-000003E&quot;,
      &quot;POS20-000046A&quot;
]
Don't aggregate the whole row, just aggregate the column:
SELECT json_agg(invoice_number) 
FROM 
(
  SELECT DISTINCT invoice_number 
  FROM sale.invoice_item 
  WHERE product_id = ANY(_product_ids)
) record

Or even simpler:
SELECT json_agg(DISTINCT invoice_number)
FROM sale.invoice_item 
WHERE product_id = ANY(_product_ids)