python - 将嵌套的 JSON 字符串展平到 Google BigQuery 中的不同列

标签 python json pandas google-bigquery

我在其中一个 BigQuery 表中有列,如下所示。

{"name": "name1", "last_delivered": {"push_id": "push_id1", "time": "time1"}, "session_id": "session_id1", "source": "SDK", "properties": {"UserId": "u1"}}

在 GBQ 中有没有得到这样的输出?? (基本上把整列压平成不同的列)

name    last_delivered.push_id   last_delivered.time   session_id   source   properties.UserId

name1       push_id1                     time1         session_id1   SDK          uid1

假设

a = {"name": "name1", "last_delivered": {"push_id": "push_id1", "time": "time1"}, "session_id": "session_id1", "source": "SDK", "properties": {"UserId": "u1"}}

我曾尝试使用 json_normalize(a) 在 Pandas Python 中获得所需的输出,但每次尝试时都会出现以下错误

enter image description here

有谁知道我怎样才能得到我想要的输出。我错过了什么吗??

任何帮助将不胜感激!!

最佳答案

以下示例适用于 BigQuery 标准 SQL

#standardSQL
WITH `project.dataset.table` AS (
  SELECT '{"name": "name1", "last_delivered": {"push_id": "push_id1", "time": "time1"}, "session_id": "session_id1", "source": "SDK", "properties": {"UserId": "u1"}}' col
)
SELECT 
  JSON_EXTRACT_SCALAR(col, '$.name') name,
  STRUCT(
    JSON_EXTRACT_SCALAR(col, '$.last_delivered.push_id') AS push_id,
    JSON_EXTRACT_SCALAR(col, '$.last_delivered.time') AS time
  ) last_delivered,
  JSON_EXTRACT_SCALAR(col, '$.session_id') session_id,
  JSON_EXTRACT_SCALAR(col, '$.source') source,
  STRUCT(
    JSON_EXTRACT_SCALAR(col, '$.properties.UserId') AS UserId
  ) properties
FROM `project.dataset.table`   

并按预期/要求产生结果

Row name    last_delivered.push_id  last_delivered.time session_id  source  properties.UserId    
1   name1   push_id1                time1               session_id1 SDK     u1     

关于python - 将嵌套的 JSON 字符串展平到 Google BigQuery 中的不同列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55244750/

相关文章:

python - 如何并行处理 CSV 文件?

python - 使用 C++ Eigen 库处理 numpy 数组后输出错误

python - 如何在Python中显示没有公共(public)行的列表差异?

javascript - Angular UI-Grid TypeError : Cannot set property 'data' of undefined. 发送来自服务的 promise 和未定义的数据

python - 作为新列附加到 Pandas 中的 DataFrame

Python Scipy 优化下限(而不是 A_ub)

c# - 在Unity中序列化和反序列化Json和Json数组

php - 如何让 JSON 与 PHP 一起使用

python - 计算json数据每小时的平均值

python - 填充空值直到 Pandas 中的特定列值