简体   繁体   English

BigQuery 中 JSON 类型列的 SELECT 个值

[英]SELECT values from JSON type column in BigQuery

I have the following SQL to insert data into a JSON type column in BigQuery我有以下 SQL 将数据插入 BigQuery 中的 JSON 类型列

DROP TABLE MMP.tmpJourneys;

CREATE TABLE MMP.tmpJourneys (
  id INT64 NOT NULL,
  data JSON
);

INSERT INTO MMP.tmpJourneys (id, data)
VALUES 
(1, JSON '{"journey_id":1,"transport_type":"train","origin":"London","destination":"Manchester","origin_time":"09:00","destination_time":"12:00","duration_mins":180,"intermediate_stops":[{"station":"Birmingham","arrival_time":"10:00","departure_time":"10:15"},{"station":"Crewe","arrival_time":"12:30","departure_time":"12:45"}]}'),
(2, JSON '{"journey_id":2,"transport_type":"bus","origin":"Manchester","destination":"Liverpool","origin_time":"10:00","destination_time":"12:00","duration_mins":120,"intermediate_stops":[{"station":"Warrington","arrival_time":"11:30","departure_time":"11:45"},{"station":"StHelens","arrival_time":"13:00","departure_time":"13:15"}]}'),
(3, JSON '{"journey_id":3,"transport_type":"scooter","origin":"Liverpool","destination":"Birmingham","origin_time":"13:00","destination_time":"14:30","duration_mins":90,"intermediate_stops":[{"station":"Warrington","arrival_time":"14:30","departure_time":"14:45"}]}');

SELECT * FROM MMP.tmpJourneys;

The data represents public transport journeys with a nested group of intermediate locations.这些数据表示具有一组嵌套的中间位置的公共交通旅程。

How can I query the data in the intermediate_stops repeating group?如何查询 intermediate_stops 重复组中的数据?

SELECT JSON_EXTRACT_SCALAR(data, '$.journey_id') AS journey_id,
       JSON_EXTRACT_SCALAR(data, '$.transport_type') AS transport_type,
       JSON_EXTRACT_SCALAR(data, '$.origin') AS transport_type,
       JSON_EXTRACT_SCALAR(data, '$.destination') AS transport_type,
       JSON_EXTRACT_SCALAR(data, '$.origin_time') AS transport_type,
       JSON_EXTRACT_SCALAR(data, '$.duration_mins') AS transport_type,       
       JSON_EXTRACT(data, '$.intermediate_stops') AS intermediate_stops,
       JSON_EXTRACT_SCALAR(stops, '$.station') AS station1,
       
      --  intermediate_stops
      --  ARRAY(SELECT AS STRUCT 
      --               location, 
      --               time
      --          FROM UNNEST(JSON_EXTRACT(data, '$.intermediate_stops'))
      --       ) AS intermediate_stops
  FROM MMP.tmpJourneys,
  UNNEST(JSON_QUERY_ARRAY(data.intermediate_stops)) AS stops;

It looks you're almost close to the answer.看起来你几乎接近答案了。 Consider below query.考虑以下查询。

WITH tmpJourneys AS (
  SELECT 1 id, JSON '{"journey_id":1,"transport_type":"train","origin":"London","destination":"Manchester","origin_time":"09:00","destination_time":"12:00","duration_mins":180,"intermediate_stops":[{"station":"Birmingham","arrival_time":"10:00","departure_time":"10:15"},{"station":"Crewe","arrival_time":"12:30","departure_time":"12:45"}]}' data UNION ALL
  SELECT 2, JSON '{"journey_id":2,"transport_type":"bus","origin":"Manchester","destination":"Liverpool","origin_time":"10:00","destination_time":"12:00","duration_mins":120,"intermediate_stops":[{"station":"Warrington","arrival_time":"11:30","departure_time":"11:45"},{"station":"StHelens","arrival_time":"13:00","departure_time":"13:15"}]}' UNION ALL
  SELECT 3, JSON '{"journey_id":3,"transport_type":"scooter","origin":"Liverpool","destination":"Birmingham","origin_time":"13:00","destination_time":"14:30","duration_mins":90,"intermediate_stops":[{"station":"Warrington","arrival_time":"14:30","departure_time":"14:45"}]}'
)
SELECT JSON_VALUE(data, '$.journey_id') AS journey_id,
       JSON_VALUE(data, '$.transport_type') AS transport_type,
       -- ...
       ARRAY(
         SELECT AS STRUCT
                JSON_VALUE(e, '$.station') AS station,
                JSON_VALUE(e, '$.arrival_time') AS arrival_time,
                JSON_VALUE(e, '$.departure_time') AS departure_time
           FROM UNNEST(JSON_QUERY_ARRAY(data, '$.intermediate_stops')) e
       ) AS intermediate_stops
  FROM tmpJourneys t;

Query results查询结果

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Spark 写入 BigQuery 中的 JSON 列类型 - Writing to a JSON column type in BigQuery using Spark 从BigQuery中的JSON中的数组中提取多个值 - Extract multiple values from an array in JSON in BigQuery BigQuery:从 json 对象数组中提取选定键的值 - BigQuery: Extract values of selected keys from an array of json objects BigQuery 从 json 数据列创建表/视图 - BigQuery make a table/view from json data column 从 Bigquery 中不包括 NULL 值的列创建数组/json - Create an array/json from columns excluding NULL values in Bigquery 如何从 BigQuery 嵌套记录类型中获取值 - How to get values from BigQuery nested Record type 无法将具有 JSON/RECORD 列类型的 bigquery 表读入 spark dataframe。(java.lang.IllegalStateException:意外类型:JSON) - Unable to read bigquery table with JSON/RECORD column type into spark dataframe. ( java.lang.IllegalStateException: Unexpected type: JSON) Select 其他表作为 BigQuery 中基于日期时间的列 - Select other table as a column based on datetime in BigQuery BigQuery:Select 列如果存在,否则输入 NULL? - BigQuery: Select column if it exists, else put NULL? 如何从 BigQuery 上 SQL 中的 JSON 字符串中提取嵌套值? - How do I extract nested values from a JSON string in SQL on BigQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM