如何在 Redshift 中替换 JSON 中的值?

Posted

技术标签:

【中文标题】如何在 Redshift 中替换 JSON 中的值?【英文标题】:How can I replace values in JSON in Redshift? 【发布时间】:2020-11-26 13:32:02 【问题描述】:

我有一列包含 JSON 格式的地址信息。我想用“--”替换街道名称和门牌号。

这是我的专栏的样子:

"1": "house_no":1, "street_name":"street1",  "2": "house_no":2, "street_name":"street2"

所以我的专栏应该是这样的:

 "1": "house_no":"--", "street_name":"--",  "2": "house_no":"--", "street_name":"--"

在 Redshift 中,JSON 存储为 VARCHAR 列,所以我的假设是需要 regexp 函数。

【问题讨论】:

【参考方案1】:

你可以使用regexp_replace():

regexp_replace(regexp_replace(col, '"house_no":[^,]+', '"house_no":"--"'), '"street_name":"[^"]+"', '"street_name":"--"')

在 Redshift 中,这应该替换所有出现的情况。在大多数其他数据库中,您需要 1, 1, 'g' 进行全局替换。

【讨论】:

以上是关于如何在 Redshift 中替换 JSON 中的值?的主要内容,如果未能解决你的问题,请参考以下文章