1

在我的配置单元表“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 时,它都会忽略后面的内容并转到下一个字段:示例:

在此处输入图像描述

有谁知道如何解决这个问题?

4

1 回答 1

1

这是因为 null 没有双引号,而您将其替换'"},\\{"'为 this'"},,,,{"'

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

split(regexp_replace(substr(serviceid, 2, length(serviceid)-2),
            '},\\{"', '},,,,{"'), ',,,,')
于 2020-11-16T17:04:53.113 回答