Quantcast
Channel: Question and Answer » json
Viewing all articles
Browse latest Browse all 148

Postgres query to return JSON object keys as array

$
0
0

Is it possible to return a JSON object keys as an array of values in PostgreSQL?

In JavaScript, this would simply be Object.keys(obj), which returns an array of strings.

For example, if I have a table like this:

tbl_items
---------
id bigserial NOT NULL
obj json NOT NULL

And if there’s a row like this:

id      obj
-----   -------------------------
123     '{"foo":1,"bar":2}'

How can I have a query to return:

id      keys
-----   ------------------
123     '{"foo","bar"}'

Viewing all articles
Browse latest Browse all 148

Trending Articles