I have a postgres table that contains a jsonb-row like this:
| MyTable.refs |
|---|
[{"url": "url1", "name": "name1"}, {"url": "url2", "name": "name2"}] |
[] |
[{"url": "url3", "name": "name3"} |
I would like to write a postgres function that creates a new column like this:
| result |
|---|
['<a href="url1">name1</a>', '<a href="url2">name2</a>'] |
[] |
['<a href="url3">name3</a>'} |
I already have something:
SELECT
jsonb_agg(format('<a href="%s">%s</a>', (elem ->> 'url'), (elem ->> 'name')))
FROM jsonb_array_elements('[{"url": "url1", "name": "name1"}, {"url": "url2", "name": "name2"}]'::jsonb) elem;
Which returns:
["<a href="url1">name1</a>", "<a href="url2">name2</a>"]
How do I apply this transformation to the whole column?