I am trying to get array of JSONs via using query below. But the result is array of tuples. How can I get rid of parantheses which covers each dictionary element and make the result array of dictionaries?
SELECT row_to_json(foo)
FROM
(
SELECT ilce, mah, phone
FROM ilan
LIMIT 5
) as foo;
Query above generates the first result. How can I get the second one?
First Result:
[({'ilce': 'esenyurt', 'mah': 'mehmet akif ersoy mh.', 'phone': '05308635664'},), ({'ilce': 'esenyurt', 'mah': 'mehmet akif ersoy mh.', 'phone': '05417411067'},), ({'ilce': 'esenyurt', 'mah': 'mehmet akif ersoy mh.', 'phone': '05536253236'},), ({'ilce': 'esenyurt', 'mah': 'mehmet akif ersoy mh.', 'phone': '05308776153'},), ({'ilce': 'esenyurt', 'mah': 'mehmet akif ersoy mh.', 'phone': '05308635664'},)]
Second one:
[{'ilce': 'esenyurt', 'mah': 'mehmet akif ersoy mh.', 'phone': '05308635664'}, {'ilce': 'esenyurt', 'mah': 'mehmet akif ersoy mh.', 'phone': '05417411067'}, {'ilce': 'esenyurt', 'mah': 'mehmet akif ersoy mh.', 'phone': '05536253236'}, {'ilce': 'esenyurt', 'mah': 'mehmet akif ersoy mh.', 'phone': '05308776153'}, {'ilce': 'esenyurt', 'mah': 'mehmet akif ersoy mh.', 'phone': '05308635664'}]
Use jsonb_agg()
and to_jsonb()
SELECT jsonb_agg(to_jsonb(foo))
FROM
(
SELECT ilce, mah, phone
FROM ilan
LIMIT 5
) as foo;