Тестирование на примере ваших данных.Замените CTE своим столом.Прочитайте комментарии в коде:
with your_table as (--use your table instead of this CTE
select stack(2,
"monday",'[{"temp" : 45, "weather": "rainny"}, {"temp" : 25, "weather": "sunny"}, {"temp" : 15, "weather": "storm"}]',
"tuesday" ,'[{"temp" : 5, "weather": "winter"}, {"temp" : 10, "weather": "sun"}, {"temp" : 18, "weather": "dawn"}]'
)as (day, type_of_day)
) --use your table instead of this CTE
select s.day, array(get_json_object(type_of_day_array[0],'$.temp'),
get_json_object(type_of_day_array[1],'$.temp'),
get_json_object(type_of_day_array[2],'$.temp')
) as result_array --extract JSON elements and construct array
from
(
select day, split(regexp_replace(regexp_replace(type_of_day,'\\[|\\]',''), --remove square brackets
'\\}, *\\{','\\}##\\{'), --make convenient split separator
'##') --split
as type_of_day_array
from your_table --use your table instead of this CTE
)s;
Результат:
s.day result_array
monday ["45","25","15"]
tuesday ["5","10","18"]
См. Тест здесь: http://demo.gethue.com/hue/editor?editor=307980
Если массив JSON может содержать более трех элементов, затем вы можете использовать вид сбоку explode или posexplode, а затем построить результирующий массив, как показано в этом ответе: https://stackoverflow.com/a/51570035/2700344.
Обернуть элементы массива в cast (... как int) если вам нужно array<int>
в результате вместо array<string>
:
cast(get_json_object(type_of_day[0],'$.temp') as int)...