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.

3
  • Would be way easier to do client-side. Commented May 26, 2020 at 4:18
  • Do you have a table with the same columns? That would make it easy. Commented May 26, 2020 at 8:04
  • 1
    Does this answer your question ? Commented May 26, 2020 at 8:58

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Henriqe Ferreira - Thanks alot. It worked. I created the query with all the 100 keys as you mentioned
I'm glad I helped :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.