在字符串列 bigquery 中查询 json
Posted
技术标签:
【中文标题】在字符串列 bigquery 中查询 json【英文标题】:query json in a string column bigquery 【发布时间】:2020-02-10 14:13:51 【问题描述】:我想知道是否可以查询一个 json 字符串列,该列将取消嵌套产品而无需指定索引。
例子
with mytable as (
select
'"ecommerce":"purchase":"actionField":"id":"T12345","affiliation":"Online Store","revenue":"35.43","tax":"4.90","shipping":"5.99","coupon":"SUMMER_SALE","products":["name":"Triblend android T-Shirt","id":"12345","price":"15.25","brand":"Google","category":"Apparel","variant":"Gray","quantity":1,"coupon":"","name":"Donut Friday Scented T-Shirt","id":"67890","price":"33.75","brand":"Google","category":"Apparel","variant":"Black","quantity":1]' as eec
)
select
JSON_EXTRACT_SCALAR(eec, "$['ecommerce'].purchase.actionField.id") AS order_id,
JSON_EXTRACT_SCALAR(eec, "$['ecommerce'].purchase.products[0].name") AS product_1
from mytable
union all
select
JSON_EXTRACT_SCALAR(eec, "$['ecommerce'].purchase.actionField.id") AS order_id,
JSON_EXTRACT_SCALAR(eec, "$['ecommerce'].purchase.products[1].name") AS product_1
from mytable
预期输出
order_id product_1
T12345 Triblend Android T-Shirt
T12345 Donut Friday Scented T-Shirt
enter image description here
但我想获得这个输出而不必做产品[索引]的联合,但有一些东西会自动重复和取消嵌套产品内部的元素
【问题讨论】:
【参考方案1】:#standardSQL
CREATE TEMP FUNCTION json2array(input STRING)
RETURNS ARRAY<STRING>
LANGUAGE js AS """
return JSON.parse(input).map(x=>JSON.stringify(x));
""";
WITH mytable AS (
SELECT
'"ecommerce":"purchase":"actionField":"id":"T12345","affiliation":"Online Store","revenue":"35.43","tax":"4.90","shipping":"5.99","coupon":"SUMMER_SALE","products":["name":"Triblend Android T-Shirt","id":"12345","price":"15.25","brand":"Google","category":"Apparel","variant":"Gray","quantity":1,"coupon":"","name":"Donut Friday Scented T-Shirt","id":"67890","price":"33.75","brand":"Google","category":"Apparel","variant":"Black","quantity":1]' AS eec
)
SELECT
JSON_EXTRACT_SCALAR(eec, "$['ecommerce'].purchase.actionField.id") AS order_id,
JSON_EXTRACT_SCALAR(product, "$.name") AS product_1
FROM mytable,
UNNEST(json2array(JSON_EXTRACT(eec, "$['ecommerce'].purchase.products"))) product
有输出
Row order_id product_1
1 T12345 Triblend Android T-Shirt
2 T12345 Donut Friday Scented T-Shirt
【讨论】:
以上是关于在字符串列 bigquery 中查询 json的主要内容,如果未能解决你的问题,请参考以下文章
使用 objection.js 或 knex.js 在 postgres 中的字符串列的 json 数组中查询