Sample Header Ad - 728x90

PostgreSQL - how to generate an array containing column name and column data...?

0 votes
2 answers
4544 views
I'd like to create a dictionary - an array - from a simple query, to include column names in output. IE, I'd like to turn this: SELECT id, last, first FROM names; id | last | first -----+----------+------------------------- 001 | Smith | John into: { "id": "001","last": "Smith", "first": "John" } Yes, bonus points for the quotes and colons! Ha! We do have this very interesting catalog function, which will return all column names into an array. SELECT array_agg(column_name::TEXT) FROM information_schema.columns WHERE table_name = 'people'; How to aggregate (couldn't help myself) that into a regular query?
Asked by DrLou (121 rep)
Dec 13, 2018, 05:06 PM
Last activity: Dec 14, 2018, 02:39 PM