2017-07-19 89 views
1

如何查詢在bigquery表中存儲爲字符串的json? 我有一個表,其中一列(subscriptions)值是這樣的:如何查詢在bigquery表中存儲爲字符串的json?

{ 
    "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。

+0

@米哈伊爾 - Berlyant這是跟進從昨天的問題。你能幫忙嗎? –

+1

請注意:您可以使用@僅回覆已經參與帖子的SO用戶。否則 - 您的郵件沒有收到預期的收件人。只是閱讀更多關於SO的幫助來了解並更好地瞭解本網站的運作方式以及它周圍的規則。同時 - 我會檢查你的問題,然後回來:o) –

+1

問題的一部分是你共享的JSON無效(嘗試粘貼到https://jsonlint.com/),所以它不可能使用'JSON_EXTRACT',除非轉錄過程中出現錯誤。 –

回答

2

下面是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} 
+0

對不起,沒有看到你已經發布了基本相同的東西,所以我會刪除我的答案。如果期望它們是字符串,那麼OP可能希望'JSON_EXTRACT_SCALAR'用於兩個'features'字段。 –

+0

呃!我是依靠python的。 –

+0

@ElliottBrossard - 沒問題,對不起,你在幾秒前得到它。相信我 - 在很多情況下 - 就在我準備發佈答案之前 - 我看到你的答案剛到,所以我沒有發帖 - 在大多數情況下,它幾乎是相同的 - 我很享受我們這麼認爲的事實相似:o) –

相關問題