我有一个名为json_data
的BigQuery表,其中有一个STRING类型的列json_column
。此列中存储的JSON数据具有以下结构:
{
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"addresses": [
{
"street": "123 Main St",
"city": "New York",
"country": "USA"
},
{
"street": "456 Elm St",
"city": "Los Angeles",
"country": "USA"
}
]
}
我想将这个JSON数据插入BigQuery中名为users的嵌套表中,该表具有以下模式:
CREATE TABLE users (
id INT64,
name STRING,
email STRING,
addresses ARRAY<STRUCT<
street STRING,
city STRING,
country STRING
>>
)
有人能指导我如何写一个SQL查询来实现这一点吗?我想从json_data
表中提取JSON数据,并将其插入到具有适当结构的users
表中。
**尝试:**我尝试了下面的查询,但它没有给予预期的响应:
INSERT INTO users (id, name, email, addresses)
SELECT
JSON_EXTRACT_SCALAR(json_column, '$.id') AS id,
JSON_EXTRACT_SCALAR(json_column, '$.name') AS name,
JSON_EXTRACT_SCALAR(json_column, '$.email') AS email,
JSON_EXTRACT_ARRAY(json_column, '$.addresses') AS addresses
FROM json_data
但我得到一个错误:
Query column 4 has type ARRAY<STRING> which cannot be inserted into column addresses, which has type ARRAY<STRUCT<street STRING, city STRING, country STRING>> at [2:1]
任何帮助将不胜感激。谢谢你!
1条答案
按热度按时间8nuwlpux1#
我认为你应该尝试下面的东西:
大多数事情都是显而易见的,但下面解释了结构体提取的数组: