I have a json data where the location is the array colum derived with below values
["USA","China","India","UK"]
["Nepal","China","India","UK","Japan"]
I need a simple sql query to explode the array column and then pivot into dynamic number of columns based on the number of values in the array. something like this pivot(explode(from_json(jsondata:export.location, 'array<string>'))) as loc_
SELECT
from_json(jsondata:export.location, 'array<string>') AS `Location`
pivot(explode(from_json(jsondata:export.location, 'array<string>'))) as loc_,
FROM mytable
Input
| Location |
|---|
| [China, India, UK] |
| [China, India, UK, Japan] |
Output
| Location | loc_1 | loc_2 | loc_3 | loc_4 |
|---|---|---|---|---|
| [China, India, UK] | "China" | "India" | "UK" | |
| [China, India, UK, Japan] | "China" | "India" | "UK" | "Japan" |
Please find the below simple & different solution
Find one array which has more number of elements then convert that into
'loc_1 STRING, loc_2 STRING, loc_3 STRING, loc_4 STRING'schema.Use
from_csvfunction & schema to convert string data into columns.