Given a row with Col1, Col2. How to convert the row into
[
{
"name": "Col1",
"value": "Column1Value"
},
{
"name": "Col2",
"value": "Column2Value"
}
]
``
Here it is. Substitute the_table with your actual table name.
with jsonrows(jsonobject, rownumber) as
(
select to_json(t), row_number() over ()
from the_table t
)
select json_agg(json_build_object('name', key, 'value', value))
from jsonrows
cross join lateral json_each_text(jsonobject)
group by rownumber;