如何查询在bigquery表中存储为字符串的json?
我有一个表,其中列(<SetDirectory/>
)中的值如下所示:
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_percent
和features
?
对于marketingFeatures
,我试过了
created
但它返回null。
答案 0 :(得分:5)
下面是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对features
和marketingFeatures
无效 - 它们在双引号内包含双引号 - 因此您可以看到我在示例数据中更改了此内容 - 但是您需要修复生成它的那部分应用程序
添加了原始字符串使用示例:
#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}