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

BI *_*ect 3 google-bigquery

如何查询存储在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"
}
Run Code Online (Sandbox Code Playgroud)

如何选择application_fee_percentfeatures以及marketingFeatures

对于created,我尝试过, JSON_EXTRACT("subscriptions", "$.data[0].created") 但返回的是null。

Mik*_*ant 6

以下是用于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
Run Code Online (Sandbox Code Playgroud)

结果符合预期

application_fee_percent created     currentlySelling  features marketingFeatures      
null                    1500476240  true              {'shipping': true,'transactionFee':0.00}  ['Unlimited products']   
Run Code Online (Sandbox Code Playgroud)

请注意:您的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
Run Code Online (Sandbox Code Playgroud)

结果是

features     
{"shipping": true,"productLimit":5,"discounts": false,"customDomain": false, "imageLimit": 100,"transactionFee":0.03}
Run Code Online (Sandbox Code Playgroud)