简体   繁体   English

如何在大查询中将正则表达式与JSON_EXTRACT一起使用

[英]How to use regex with JSON_EXTRACT on big query

I have a JSON field on big query table and currently i'm using the following method to do the extraction from a id element (for example): 我在大查询表上有一个JSON字段,目前我正在使用以下方法从id元素中提取(例如):

coalesce(
   nullif(JSON_EXTRACT(e.event_payload, 'content_id'), ''),
   nullif(JSON_EXTRACT(e.event_payload, 'cid'), ''),
   nullif(JSON_EXTRACT(e.event_payload, 'c_id'), ''),
   ...
  ) AS content_id,

I don't have a pattern on this JSON's fields... Is possible use REGEX with JSON_EXTRACT on big query like this? 我在此JSON字段上没有模式...是否可以在像这样的大查询上使用REGEX和JSON_EXTRACT?

JSON_EXTRACT(e.event_payload, "(content_id|cid|c_id)") as content_id

You can replace the possible field names and then perform extraction: 您可以替换可能的字段名称,然后执行提取:

SELECT
  JSON_EXTRACT(
    REGEXP_REPLACE(e.event_payload, r'"c\_?id"', '"content_id"'),
    '$.content_id') as content_id
FROM dataset.table

As a self-contained example: 作为一个独立的示例:

WITH T AS (
  SELECT '{"cid": {"a": 1}}' AS event_payload UNION ALL
  SELECT '{"content_id": {"b": 2}}' UNION ALL
  SELECT '{"c_id": {"c": 3}}'
)
SELECT
  JSON_EXTRACT(
    REGEXP_REPLACE(e.event_payload, r'"c\_?id"', '"content_id"'),
    '$.content_id') as content_id
FROM T AS e

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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