JSON_ARRAY_ELEMENTS (Lakehouse v1)
Extrait les éléments d’un tableau JSON et les renvoie sous forme de lignes distinctes dans le jeu de résultats. JSON_ARRAY_ELEMENTS ne développe pas récursivement les tableaux imbriqués : il les traite comme des éléments uniques.
Syntaxe Analyze
Section intitulée « Syntaxe Analyze »func.json_array_elements(<json_string>)Exemples Analyze
Section intitulée « Exemples Analyze »func.json_array_elements(func.parse_json('[ \n {"product": "laptop", "brand": "apple", "price": 1500},\n {"product": "smartphone", "brand": "samsung", "price": 800},\n {"product": "headphones", "brand": "sony", "price": 150}\n]'))┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐│ func.json_array_elements(func.parse_json('[ \n {"product": "laptop", "brand": "apple", "price": 1500},\n {"product": "smartphone", "brand": "samsung", "price": 800},\n {"product": "headphones", "brand": "sony", "price": 150}\n]')) │├───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤│ {"brand":"Apple","price":1500,"product":"Laptop"} ││ {"brand":"Samsung","price":800,"product":"Smartphone"} ││ {"brand":"Sony","price":150,"product":"Headphones"} │└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘Syntaxe SQL
Section intitulée « Syntaxe SQL »JSON_ARRAY_ELEMENTS(<json_string>)Type de retour
Section intitulée « Type de retour »JSON_ARRAY_ELEMENTS renvoie un ensemble de valeurs VARIANT, chacune représentant un élément extrait du tableau JSON en entrée.
Exemples SQL
Section intitulée « Exemples SQL »-- Extract individual elements from a JSON array containing product informationSELECT JSON_ARRAY_ELEMENTS( PARSE_JSON ( '[ {"product": "Laptop", "brand": "Apple", "price": 1500}, {"product": "Smartphone", "brand": "Samsung", "price": 800}, {"product": "Headphones", "brand": "Sony", "price": 150}]' ) );
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐│ json_array_elements(parse_json('[ \n {"product": "laptop", "brand": "apple", "price": 1500},\n {"product": "smartphone", "brand": "samsung", "price": 800},\n {"product": "headphones", "brand": "sony", "price": 150}\n]')) │├─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤│ {"brand":"Apple","price":1500,"product":"Laptop"} ││ {"brand":"Samsung","price":800,"product":"Smartphone"} ││ {"brand":"Sony","price":150,"product":"Headphones"} │└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
-- Display data types of the extracted elementsSELECT TYPEOF ( JSON_ARRAY_ELEMENTS( PARSE_JSON ( '[ {"product": "Laptop", "brand": "Apple", "price": 1500}, {"product": "Smartphone", "brand": "Samsung", "price": 800}, {"product": "Headphones", "brand": "Sony", "price": 150}]' ) ) );
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐│ typeof(json_array_elements(parse_json('[ \n {"product": "laptop", "brand": "apple", "price": 1500},\n {"product": "smartphone", "brand": "samsung", "price": 800},\n {"product": "headphones", "brand": "sony", "price": 150}\n]'))) │├─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤│ VARIANT NULL ││ VARIANT NULL ││ VARIANT NULL │└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘