我需要全局替换一个在嵌套的JSON结构中出现多次的特定字符串,该结构以jsonb形式存储在Postgres表中。例如:
{
"location": "tmp/config",
"alternate_location": {
"name": "config",
"location": "tmp/config"
}
}
...应该变成:
{
"location": "tmp/new_config",
"alternate_location": {
"name": "config",
"location": "tmp/new_config"
}
}
我尝试过:
UPDATE files SET meta_data = to_json(replace(data::TEXT, 'tmp/config', 'tmp/new_config'));
不幸的是,这会导致 JSON 格式不正确,出现三重转义的引号。
有什么想法如何解决这个问题吗?