JSON_ARRAY_ELEMENTS (Lakehouse v1)
Wyodrębnia elementy tablicy JSON i zwraca je jako osobne wiersze zbioru wyników. JSON_ARRAY_ELEMENTS nie rozwija rekurencyjnie zagnieżdżonych tablic; traktuje je jako pojedyncze elementy.
Składnia Analyze
Dział zatytułowany „Składnia Analyze”func.json_array_elements(<json_string>)Przykłady Analyze
Dział zatytułowany „Przykłady 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"} │└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘Składnia SQL
Dział zatytułowany „Składnia SQL”JSON_ARRAY_ELEMENTS(<json_string>)Typ zwracany
Dział zatytułowany „Typ zwracany”JSON_ARRAY_ELEMENTS zwraca zbiór wartości VARIANT, z których każda reprezentuje element wyodrębniony z wejściowej tablicy JSON.
Przykłady SQL
Dział zatytułowany „Przykłady 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 │└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘