Reputation: 5469
My table:
ID | something1 | something2 | ...
1 | meow | 5 |
2 | 4 | KITTIES |
Is there any way to select data as JSON in format {"1":{"something1":"meow","something2":5},"2":{...}}
?
Upvotes: 1
Views: 224
Reputation: 5537
If you don't mind repeating the ID field in the JSON representation of a row, you can do:
SELECT
format('{%s}',
string_agg(
format(
'%s:%s',
to_json(ID::text),
row_to_json(my_table)
), ','
), ''
)::json as json_object
FROM my_table;
This gives you a JSON object containing a sub-object for each row in the table, keyed by the value in the ID field.
See this question for more details.
Upvotes: 1
Reputation: 3393
You can use this library to get an API of the database. Then, consume it! This is the fastest and clearest thing I can imagine.
Upvotes: 1