json - 从 BigQuery 中的字符串化 JSON 解析时间戳对象

标签 json google-cloud-firestore google-bigquery

我在 BigQuery 中有一个字符串化的 JSON 列,如下所示:

{"status":0,"buyEquipment":false,"created":{"_seconds":1603578346,"_nanoseconds":909000000},"total":0,"events":{"1603578346909":{"status":0}},"approvalStatus":0,"userId":"xAdkXoah6LMXI4xy9SZ14NUGJIH3","facilityId":"-MF_DJYYyRKbW4fs91v_","orderSize":0,"facility":{"name":"Data Center ehf.","photo":"-MF_cjRQ3dWW3NRMJC6I","province":"Southern Peninsula","city":"Reybaer"},"priceKWh":0.01}

我试图从中提取某些值,如下所示:

SELECT
JSON_EXTRACT(data, '$.created') AS Date_Created
FROM table

我得到这样的东西:

{"_seconds":1607095273,"_nanoseconds":847000000}

因为它显然是嵌套的。我不熟悉从 (Firestore) 导入的数据库如何处理时间戳对象,但这就是将其导入 BigQuery 的方式。

我也愿意

  • 如果可能,使用单个 BigQuery 或标准 SQL 命令将其转换为更熟悉的时间戳格式,
  • 或者如果不只是将其提取为一种格式,那么它可以在 BigQuery 中轻松转换为人类可读的日期。

我尝试了各种 BigQuery 内置函数,但均无济于事。谢谢!

最佳答案

I would like to ... just extract it to a format where it is an easy conversion to human readable date in BigQuery.

在下面使用(非常不言自明)

select 
  date(timestamp_seconds(cast(json_extract(data, '$.created._seconds') as int64))) AS Date_Created
from table         

如果应用于您问题中的样本数据 - 输出是

enter image description here

关于json - 从 BigQuery 中的字符串化 JSON 解析时间戳对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67549834/

相关文章:

firebase - Firestore 嵌套安全规则

python - 包装库函数以重试 500 错误无法正常工作

google-bigquery - BigQuery 计划查询(脚本)循环 : needs destination table to be saved, 但由于设置目标表而运行失败

json - 将速度响应转换为 JSON

javascript - 用 $http.get() 数据填充 Chart js

json - 将层次结构从派生列写入不带列名称的 JSON 接收器 Azure Dataflow

android - Resttemplate 安卓 : RestClientException: Could not extract response: no suitable HttpMessageConverter found

firebase - 如果在 Flutter 应用程序中是否存在 firestore 数据库集合,如何检查文档是否存在?

javascript - 无法将 “custom blob” 保存到 Firestore - ADMIN SDK

SQL 查询在一系列重叠(时间)间隔内查找非重叠间隔的子系列