【问题标题】:How to query json stored as string in bigquery table?如何查询在bigquery表中存储为字符串的json?
【发布时间】:2017-12-25 05:32:00
【问题描述】:

如何查询以字符串形式存储在 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") 但它返回 null。

【问题讨论】:

  • 部分问题是您共享的 JSON 无效(尝试将其粘贴到 jsonlint.com),因此除非有可能,否则无法使用 JSON_EXTRACT转录错误。
  • 那是我的错。我有一个很大的价值,我试图让它更小,但我搞砸了。更新它,新的json是有效的。

标签: google-bigquery


【解决方案1】:

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

#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}

【讨论】:

  • 抱歉,没有看到您已经发布了基本相同的内容,所以我将删除我的答案。如果两个 features 字段应该是字符串,则 OP 可能需要 JSON_EXTRACT_SCALAR
  • 啊!为此,我依赖 python。
  • @ElliottBrossard - 没问题,很抱歉在你之前几秒钟就开始了。相信我 - 在很多情况下 - 就在我即将发布答案之前 - 我看到你的答案刚刚出现所以我没有发布 - 在大多数情况下它几乎是相同的 - 我很享受我们认为如此相似的事实: o)
  • 我想知道您对此的看法。我从源代码"features": "{\"shipping\": true,\"productLimit\":5,\"discounts\": false,\"customDomain\": false, \"imageLimit\": 100,\"transactionFee\":0.03} " 得到这个,我应该如何处理这个,以便我可以使用 json_extract 方法?
  • 不知何故,我真的怀疑您的表格中是否确实有该文本,但看起来您是从 select * 或 preview where ` just used to escape quotes inside quotes. meantime if you want to apply json_extract to such string - you should use raw string` 之类的输出中复制它 - 请参阅@ 987654321@。另请参阅我即将在我的答案中添加的示例
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多