How to get column name as json name in row_to_json in PostgreSQL

my next request for selection

select row_to_json(row(productid, product)) from tbl_pdts;


this will return the result as

{"f1":3988,"f2":"MYCET TAB"}
{"f1":3989,"f2":"MYCET COLD TAB"}
{"f1":3990,"f2":"TANDAM TAB"}"
{"f1":3991,"f2":"ACLOPEN 100 DT TAB"}
{"f1":3992,"f2":"COBEND TAB"}


but i need to get it like

{"productid":3988,"product":"MYCET TAB"}
{"productid":3989,"product":"MYCET COLD TAB"}
{"productid":3990,"product":"TANDAM TAB"}
{"productid":3991,"product":"ACLOPEN 100 DT TAB"}
{"productid":3992,"product":"COBEND TAB"}



source to share

1 answer

select row_to_json(t)
from (
  select productid, product from tbl_pdts
) t




All Articles