RedShift: не могу сделать Unnest для JSON
Всем привет.
Мне надо распарсить JSON в RedShift.
Строка, которую распарсить, находится в колонке "inputs" и содержит такое:
[{"desc": "How many people does the video contain?", "name": "Number of People", "type": "dropdown", "values": ["", "Only 1", "2-3", "3+"]}, {"desc": "What is the camera position?", "name": "Movement", "type": "dropdown", "values": ["", "Fixed position", "Moving"]}, {"desc": "From which angle did you shoot the video?", "name": "Shoot Angle", "type": "dropdown", "values": ["", "Frontal recording", "Tight angle: 10-40 degree", "Wide angle: 40-70 degree"]}, {"desc": "From which distance did you shoot the video?", "name": "Distance", "type": "dropdown", "values": ["", "Near/Selfie", "Mid (3-6 ft)", "Far (>6 ft)"]}, {"desc": "What is the video lighting direction?", "name": "Lighting Direction", "type": "dropdown", "values": ["", "Front lit", "Side lit", "Back lit"]}, {"desc": "What is the video background?", "name": "Background", "type": "dropdown", "values": ["", "Outdoors", "In office", "At home", "Plain background"]}, {"desc": "What is the topic in your speech?", "name": "Topic", "type": "dropdown", "values": ["", "Arts and Media", "Business", "Education", "Entertainment", "Food/Eating", "Nutrition", "Healthcare ", "High School Life", "Mental Health", "News", "Technology", "Morals and Ethics", "Phones and Apps", "Sports", "Science"]}]
Мне нужно так распарсить JSON, чтобы вытащить каждое из уникальных значений и сформировать отдельные строки с ними.
Например:
id: 1, desc: "How many people does the video contain?"
id: 2, desc: "What is the camera position?"
etc.
Я пробую запрос:
SELECT c.*, d.desc, d.name, d.values FROM source.table AS c, c.inputs AS d;
И получаю ошибку: navigation on column "inputs" is not allowed as it is not SUPER type
Этот запрос я вычитал в официальной документации RedShift: https://docs.aws.amazon.com/redshift/latest/dg/query-super.html
Но с моими текущими данными он не работает корректно.
Когда я попробовал применить то, что описано в их документации, и добавил туда одну из строк из своих данных
CREATE TABLE test_parse_json_super
(
id smallint,
details super
);
INSERT INTO test_parse_json_super VALUES(1, JSON_PARSE('[{"desc": "How many people does the video contain?", "name": "Number of People", "type": "dropdown", "values": ["", "Only 1", "2-3", "3+"]}, {"desc": "What is the camera position?", "name": "Movement", "type": "dropdown", "values": ["", "Fixed position", "Moving"]}, {"desc": "From which angle did you shoot the video?", "name": "Shoot Angle", "type": "dropdown", "values": ["", "Frontal recording", "Tight angle: 10-40 degree", "Wide angle: 40-70 degree"]}, {"desc": "From which distance did you shoot the video?", "name": "Distance", "type": "dropdown", "values": ["", "Near/Selfie", "Mid (3-6 ft)", "Far (>6 ft)"]}, {"desc": "What is the video lighting direction?", "name": "Lighting Direction", "type": "dropdown", "values": ["", "Front lit", "Side lit", "Back lit"]}, {"desc": "What is the video background?", "name": "Background", "type": "dropdown", "values": ["", "Outdoors", "In office", "At home", "Plain background"]}, {"desc": "What is the topic in your speech?", "name": "Topic", "type": "dropdown", "values": ["", "Arts and Media", "Business", "Education", "Entertainment", "Food/Eating", "Nutrition", "Healthcare ", "High School Life", "Mental Health", "News", "Technology", "Morals and Ethics", "Phones and Apps", "Sports", "Science"]}]'));
и использовал запрос из документации "SELECT c.*, d.desc, d.name, d.values FROM test_parse_json_super AS c, c.details AS d;"
то это работает и все данные раскладываются по строкам нормально (как и сам JSON корректный).
Не могу понять что мне надо сделать, чтобы запрос работал на моих реальных данных в реальной таблице, а не тестовой?
Может кто подскажет?
Заранее спасибо!
Ответы (1 шт):
Сделал так:
SELECT c.*, d.desc, d.name, d.values
FROM (
SELECT id, created, JSON_PARSE(inputs) AS inputs_super
FROM source.table
WHERE prompttype = 'input'
) AS c,
c.inputs_super AS d