[英]SQL - regexp_extract JSON string
我正在嘗試從以下字符串中提取 locationID
{"type":"player","topic_id":"555","topic_name":"sfd","userId":116,"userLocation":{"countryCode":"BR","locationId":21,"locationCity":"Rio de Janeiro"}}
例如,我可以使用以下 safe_cast( topic_id
safe_cast(regexp_extract(h.events.label,r'"topic_id":"([a-zA-Z0-9-_. ]+)"') as int64
提取 topic_id但這對locationId
不起作用。我猜這是因為嵌套的字典?但不確定如何解決這個問題。
您最好使用json function 而不是正則表達式function。
WITH sample_data AS (
SELECT '{"type":"player","topic_id":"555","topic_name":"sfd","userId":116,"userLocation":{"countryCode":"BR","locationId":21,"locationCity":"Rio de Janeiro"}}' json
)
SELECT CAST(JSON_VALUE(json, '$.userLocation.locationId') AS INT64) AS locationId
FROM sample_data;
+------------+
| locationId |
+------------+
| 21 |
+------------+
這不適用於
locationId
。 我猜這是因為嵌套的字典?
topic_id
的值是一個字符串"555"
,而locationId
的值是一個 integer 21
。r'"locationId":([a-zA-Z0-9-_. ]+)'
將適用於locationId
但更簡單的正則表達式將是r'"locationId":(\d+)'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.