user3839717
user3839717

Reputation:

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

following is my select query

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 as

{"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"}

Upvotes: 6

Views: 2938

Answers (1)

Vivek S.
Vivek S.

Reputation: 21915

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

Upvotes: 7

Related Questions