gpt4 book ai didi

google-bigquery - 如何查询以字符串形式存储在 bigquery 表中的 json?

转载 作者:行者123 更新时间:2023-12-01 10:28:38 25 4
gpt4 key购买 nike

如何查询以字符串形式存储在 bigquery 表中的 json?
我有一个表,其中列 ( subscriptions ) 中的值如下所示:

{
"data": [{
"application_fee_percent": null,
"canceled_at": null,
"created": 1500476240,
"items": {
"data": [{
"created": 1500476240,
"id": "s4nQMWJn4P1Lg",
"metadata": {},
"object": "subscription_item",
"plan": {
"amount": 3,
"created": 1494270926,
"currency": "usd",
"livemode": true,
"metadata": {
"currentlySelling": "true",
"features": "{\"shipping\": true,\"transactionFee\":0.00}",
"marketingFeatures": "[\"Unlimited products\"]"
},
"name": "Personal",
"object": "plan",
"statement_descriptor": null,
"trial_period_days": null
},
"quantity": 1
}],
"has_more": false,
"object": "list",
"total_count": 1,
"url": "/v1/subscri3XwjA3"
},
"livemode": true,
"metadata": {
"test": "596f735756976"
},
"object": "suion",
"quantity": 1
}],
"has_more": false,
"object": "list",
"total_count": 1,
"url": "/v1/cutions"
}

如何选择 application_fee_percentfeaturesmarketingFeatures

对于 created ,我试过 JSON_EXTRACT("subscriptions", "$.data[0].created")但它返回空值。

最佳答案

以下是 BgQuery SQL(请注意答案底部的注释!)



#standardSQL
WITH yourTable AS (
SELECT '''
{
"data": [{
"application_fee_percent": null,
"canceled_at": null,
"created": 1500476240,
"items": {
"data": [{
"created": 1500476240,
"id": "s4nQMWJn4P1Lg",
"metadata": {},
"object": "subscription_item",
"plan": {
"amount": 2500,
"created": 1494270926,
"currency": "usd",
"id": "182463d635c6b6e43",
"interval": "month",
"interval_count": 1,
"livemode": true,
"metadata": {
"currentlySelling": "true",
"features": "{\'shipping\': true,\'transactionFee\':0.00}",
"marketingFeatures": "[\'Unlimited products\']"
},
"name": "Personal",
"object": "plan",
"statement_descriptor": null,
"trial_period_days": null
},
"quantity": 1
}],
"has_more": false,
"object": "list",
"total_count": 1,
"url": "/v1/subscri3XwjA3"
},
"livemode": true,
"metadata": {
"test": "596f735630012756976"
},
"object": "subscription",
"quantity": 1,
"start": 1500476240,
"status": "trialing",
"tax_percent": 0.0,
"trial_end": 1501685839,
"trial_start": 1500476240
}],
"has_more": false,
"object": "list",
"total_count": 1,
"url": "/v1/cutions"
}
''' AS subscriptions
)
SELECT
JSON_EXTRACT_SCALAR(subscriptions, "$.data[0].application_fee_percent") AS application_fee_percent,
JSON_EXTRACT_SCALAR(subscriptions, "$.data[0].created") AS created,
JSON_EXTRACT_SCALAR(subscriptions, "$.data[0].items.data[0].plan.metadata.currentlySelling") AS currentlySelling,
JSON_EXTRACT_SCALAR(subscriptions, "$.data[0].items.data[0].plan.metadata.features") AS features,
JSON_EXTRACT_SCALAR(subscriptions, "$.data[0].items.data[0].plan.metadata.marketingFeatures") AS marketingFeatures
FROM yourTable

结果如预期

application_fee_percent created     currentlySelling  features marketingFeatures      
null 1500476240 true {'shipping': true,'transactionFee':0.00} ['Unlimited products']

请注意:您的 JSON 对 featuresmarketingFeatures 无效——它们包含双引号内的双引号——所以你可以看到我在你的示例数据中改变了这一点——但你需要修复产生它的应用程序的那部分

添加了原始字符串使用示例:

#standardSQL
WITH YourTable AS (
SELECT R"""{
"features": "{\"shipping\": true,\"productLimit\":5,\"discounts\": false,\"customDomain\": false, \"imageLimit\": 100,\"transactionFee\":0.03} "
}""" AS subscriptions
)
SELECT
JSON_EXTRACT_SCALAR(subscriptions, '$.features') AS features
FROM YourTable

结果是

features     
{"shipping": true,"productLimit":5,"discounts": false,"customDomain": false, "imageLimit": 100,"transactionFee":0.03}

关于google-bigquery - 如何查询以字符串形式存储在 bigquery 表中的 json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45199120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com