I have a JSON_TEXT column in my PostgreSQL DB such as this {‘a’:’one’, ‘b’:’two’, ‘c’:’three’}
I would like to create a function that would loop through all of the DISTINCT JSON_object_keys and create a column for each of the keys, and populate all of the values into their new columns. psuedo code example:
create or replace function myFunction (input json_text)
returns //not sure as $$//
BEGIN
// foreach(key in input)
// make and return a column populated with its values somehow idk
END; $$
I understand you can hard code the names of each key and create attributes for them but I have hundreds of keys so this wont be feasible for me.
2
Answers
Found the best answer to this was the following. this is somewhat similar to Eduard's answer just a bit of a different approach.
this took a JSON text column and put every one of its keys and their associated values into their own columns in a new table, ta-da.
Your request looks like a pivot table with a list of columns not defined at the run time. You can get your result by creating a composite type dynamically corresponding to the list of json keys and then by using the standard json function json_to_record :