Select query in row_to_json function

To work around this we must either create a row type and cast the row to that type or use a subquery. A subquery will typically be easier.

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

If one wants to prevent a sub-query, json_build_object() might be a solution. It does not map the column name, but let's your set the JSON keys explicitly.


Query

SELECT json_build_object('productid', productid, 'product', product) FROM gtab04;

json_build_object                                
------------------
{"productid":3029,"product":"DIBIZIDE M TAB"}
{"productid":3026,"product":"MELMET 1000 SR TAB"}
{"productid":2715,"product":"GLUCORED FORTE"}  
{"productid":3377,"product":"AZINDICA 500 TAB"}

View on DB Fiddle