regex_replace 函数跳过 NULL 值之后的任何内容

regex_replace function skips anything coming after a NULL value

在我的配置单元 table“ticket_full”中,我有一个名为“service_id”的 json 类型列,我想将其提取为 3 列,即像这样

[{"position":"1","typeid":"ROUTNAME","value":"PWAW13197"},{"position":"2","typeid":"CDCNAME","value":null},{"position":"3","typeid":"SVCNAME","value":"Business"},{"position":"4","typeid":"USID","value":"FI021MLQE4"}]

[{"position":"1","typeid":"ROUTNAME","value":"KHLA30076"},{"position":"2","typeid":"CDCNAME","value":"eff-e-rjh-sw-cs2"},{"position":"3","typeid":"SVCNAME","value":"Managed LAN"},{"position":"4","typeid":"USID","value":"SA00BNGH0E"}]

[{"position":"1","typeid":"NUMLIAPTT","value":"0492212984"},{"position":"2","typeid":null,"value":null},{"position":"3","typeid":null,"value":null},{"position":"4","typeid":null,"value":null}]

我使用了下面的代码:

SELECT get_json_object(single_json_table.identifiant_produit, '$.position') AS position,
  get_json_object(single_json_table.identifiant_produit, '$.typeid') AS typeid,
  get_json_object(single_json_table.identifiant_produit, '$.value') AS value
  FROM   
(SELECT explode(split(regexp_replace(substr(serviceid, 2, length(serviceid)-2),
            '"},\{"', '"},,,,{"'), ',,,,')  ) as identifiant_produit 
  FROM ticket_full) single_json_table

它有效,但每次有一个 NULL 值时,它都会忽略后面的内容并转到下一个字段: 示例:

请问有人知道如何解决这个问题吗?

这是因为 null 没有双引号,你正在用这个 '"},,,,{"'

替换这个 '"},\{"'

尝试删除正则表达式模式中 } 之前的双引号并相应地替换字符串,然后它也可以处理带引号的值和空值:

split(regexp_replace(substr(serviceid, 2, length(serviceid)-2),
            '},\{"', '},,,,{"'), ',,,,')