简体   繁体   中英

How to query where from dictionary column with datatype string in bigquery?

Hi I have a column where it contains dictionary that has a data type string. How do I query where attributes.statusSource = source in bigquery sql? Here is an example

id attributes
1 {"flushOut": 0, "expiration": 0, "productAge": 0, "sourceStatus": "SOURCE", "criticalStatus": false, "storageHandling": "FROZEN", "expirationReminder": 0}
2 {"flushOut": 0, "expiration": 0, "productAge": 0, "sourceStatus": "DISCONTINUE", "criticalStatus": false, "storageHandling": "FROZEN", "expirationReminder": 0}

what I want returned is where attribute.sourceStatus = "source"

id attributes
1 {"flushOut": 0, "expiration": 0, "productAge": 0, "sourceStatus": "SOURCE", "criticalStatus": false, "storageHandling": "FROZEN", "expirationReminder": 0}

Use JSON_EXTRACT_SCALAR() :

SELECT * FROM (
SELECT '{"flushOut": 0, "expiration": 0, "productAge": 0, "sourceStatus": "SOURCE", "criticalStatus": false, "storageHandling": "FROZEN", "expirationReminder": 0}' as attributes
UNION ALL
SELECT '{"flushOut": 0, "expiration": 0, "productAge": 0, "sourceStatus": "DISCONTINUE", "criticalStatus": false, "storageHandling": "FROZEN", "expirationReminder": 0}' as attributes
) WHERE JSON_EXTRACT_SCALAR(attributes, '$.sourceStatus') = 'SOURCE'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM