BigQuery Json嵌套

时间:2018-12-04 11:43:49

标签: json google-bigquery

我在BigQuery中有一个表,其中有两列为LOAD_DATE和JSON_OBJ。 JSON_OBJ列包含打击格式的数据。该表有大约2000条记录。我正在尝试取消JSON_OBJ的嵌套。

{
  "dataset": [
    {
      "id": 106,
      "txn_date": "2018-01-04 19:22:13",
      "txn_type": "NA",
      "txn_amount": 2000
    },
    {
      "id": 106,
      "txn_date": "2018-01-12 13:11:21",
      "txn_type": "NA",
      "txn_amount": 1100
    },
    {
      "id": 106,
      "txn_date": "2018-01-03 14:22:29",
      "txn_type": "NA",
      "txn_amount": 2000
    },
    {
      "id": 106,
      "txn_date": "2018-01-15 09:34:45",
      "txn_type": "NA",
      "txn_amount": 110
    }
  ]
}

预期输出应如下所示。

id   txn_date              txn_type txn_amount  
---  --------------        -------   ----------- 
106  2018-01-04 19:22:13   NA        2000
106  2018-01-12 13:11:21   NA        1100
106  2018-01-03 14:22:29   NA        2000
106  2018-01-15 09:34:45   NA        110

我是BigQuery的新手,我尝试使用以下查询,但这不能解决我的问题。

with mytable as
(
SELECT '{"dataset":[{"id":106,"txn_date":"2018-01-04 19:22:13","txn_type":"NA","txn_amount":2000},{"id":106,"txn_date":"2018-01-12 13:11:21","txn_type":"NA","txn_amount":1100},{"id":106,"txn_date":"2018-01-03 14:22:29","txn_type":"NA","txn_amount":2000},{"id":106,"txn_date":"2018-01-15 09:34:45","txn_type":"NA","txn_amount":110}]}' as txn_json
)
select JSON_EXTRACT_SCALAR(txn_json, "$.dataset[0].id") AS id,
JSON_EXTRACT_SCALAR(txn_json, "$.dataset[0].txn_date") AS txn_date,
JSON_EXTRACT_SCALAR(txn_json, "$.dataset[0].txn_type") AS txn_type,
JSON_EXTRACT_SCALAR(txn_json, "$.dataset[0].txn_amount") AS txn_amount
from mytable

能帮我吗?

谢谢

0 个答案:

没有答案