PostgreSQL table to json -



PostgreSQL table to json -

i have table

key | value -----+------- | "foo" b | "bar"

how can result

{"a": "foo", "b": "bar"}

it doesn't work with

select array_to_json(array_agg(t)) table t; -- [{"key": "a", "value": "foo"}, {"key": "b", "value": "bar"}]

can help me?

postgresql ≥ 9.4 :

select json_object(array_agg(key), array_agg(value)) t; ┌────────────────────────────┐ │ json_object │ ├────────────────────────────┤ │ {"a" : "foo", "b" : "bar"} │ └────────────────────────────┘ (1 row)

json postgresql

Comments

Popular posts from this blog

Using Android Volley to post an array to PHP -

python - How to add an model instance to a django queryset? -

angularjs - No 'Access-Control-Allow-Origin' error from local domain to public API -