0

I'm new and still learning.

I have a table in jsonB format:

jsonb input

And I need to output be like:

Output

Is there a easy way to do this. The jsonb has around 100 key. So it would be converted to 100 columns.

Is there a way to do this dynamically. I mean, if the number of columns or column name change, it´s not necessary to update the query.

Breno1982
  • 45
  • 4

1 Answers1

1

No,

For each key you need to create a column. To get all keys listed you can use this command:

select 
    jsonb_object_keys(u) 
from YOURTABLE;

After get the list of keys, I suggest you to use a text editor, to make easier the process. See this post certainly will help you.

After created your template you will need to do something like this:

SELECT 
   id, 
   u->'prb0' as prb0,
   u->'prb1' as prb1,
   u->'prb1' as prb2,
   ....
FROM YOUR_TABLE;

I strongly recommend you to create an intermediate table to store this query, and make it easier for furthers queries.