I have a large JSON column that I would like to break into two columns with multiple records instead of a separate column for each component. Example of the way data looks below.
Data | Json |
---|---|
1 | [{"Q1":"A1", "Q2":"A2", "Q3":"A3"}] |
2 | [{"Q1":"A4", "Q2":"A5", "Q3":"A6"}] |
I can break it down easily into separate columns named for each question, but for some reason am hitting a block at finding out the right way to break it into just two columns. Example of structure I am looking to create below.
Data | Question | Answer |
---|---|---|
1 | Q1 | A1 |
1 | Q2 | A2 |
1 | Q3 | A3 |
2 | Q1 | A4 |
2 | Q2 | A5 |
2 | Q3 | A6 |
2
Answers
This is a perfect use case for the
json_each
function:Or if there’s any number of objects in the array, combine with
json_array_elements
:Just to add to @Bergi, probably you would want to use json_each_text since your values are known to be string: