How can I get all keys from a JSON column in Postgres?

Use this:

select jsonb_object_keys(json_stuff) from table;

(Or just json_object_keys if you're using just json.)

The PostgreSQL json documentation is quite good. Take a look.

And as it is stated in the documentation, the function only gets the outer most keys. So if the data is a nested json structure, the function will not return any of the deeper keys.


WITH t(json_stuff) AS ( VALUES
  ('{"things": "stuff"}'::JSON),
  ('{"more_things": "more_stuff"}'::JSON)
)
SELECT array_agg(stuff.key) result 
FROM t, json_each(t.json_stuff) stuff;