Quantcast
Viewing latest article 2
Browse Latest Browse All 2

PostgreSQL output to JSON object/array

I'm running PostgreSQL and are trying to convert this:

SELECT car_id AS id, car_model AS model FROM cars

into this:

[{ "id" : 123, "model" : "Fiat" }, {"id": 124, "model": "BMW"}, ...]

I've tried:

SELECT json_agg(
           json_build_object(car_id AS id),
           json_build_object(car_model AS model)
       ) FROM cars

and a lot other stuff, but seems to be stuck


Viewing latest article 2
Browse Latest Browse All 2

Trending Articles