跳到主要内容
翻译页面

本页面由 AI 辅助从英文翻译而来。如两者存在差异,以英文版本为准。

JSON_REPLACE

根据路径更新 JSON 对象中的现有值。JSON_REPLACE 不会向文档添加新键,只会替换现有键的值。

另请参阅:
[SELECT] [JSON_EXTRACT] [JSON_SET] [JSON_REPLACE]


语法​

SELECT JSON_REPLACE(json_column, path, new_value) FROM <multipartIdentifier>;

参数​

json_column
要替换其中的值的 JSON 列或表达式。

path
JSON 路径表达式,指向 JSON 文档中要进行替换的位置。该路径必须指向现有键。

new_value
要在 JSON 文档的指定路径处替换成的新值。

返回值​

返回替换了指定值的已修改 JSON 文档。如果指定路径不存在,则原样返回原始 JSON 文档。

键已存在时的行为
函数已存在时是否覆盖?不存在时是否创建?
json_insert()否是
json_replace()是否
json_set()是是

示例​

替换 JSON 对象中的单个值​

SELECT sku as original,
JSON_REPLACE(sku, '$.name', 'Standard_EU') as updated
FROM azure.container_registry.registries
WHERE registry_name = 'exampleRegistryProd'
AND subscription_id = '12345678-abcd-1234-abcd-1234abcd5678'
AND resource_group_name = 'exampleResourceGroupProd';
/* expected output...
|---------------------------------------|------------------------------------------|
| original | updated |
|---------------------------------------|------------------------------------------|
| {"name":"Standard","tier":"Standard"} | {"name":"Standard_EU","tier":"Standard"} |
|---------------------------------------|------------------------------------------|
*/

替换 JSON 对象中的多个值​

select
properties beforechange,
json_replace(json_remove(properties,
'$.allowPort25Out',
'$.auxiliarySku',
'$.provisioningState',
'$.resourceGuid',
'$.macAddress',
'$.vnetEncryptionSupported',
'$.enableIPForwarding',
'$.defaultOutboundAccess',
'$.primary',
'$.virtualMachine',
'$.hostedWorkloads',
'$.tapConfigurations',
'$.nicType',
'$.auxiliaryMode',
'$.ipConfigurations[0].id',
'$.ipConfigurations[0].etag',
'$.ipConfigurations[0].type',
'$.ipConfigurations[0].properties.provisioningState',
'$.ipConfigurations[0].properties.privateIPAddress',
'$.ipConfigurations[0].properties.privateIPAllocationMethod',
'$.ipConfigurations[0].properties.primary',
'$.ipConfigurations[0].properties.privateIPAddressVersion'
),
'$.ipConfigurations[0].name',
'vmss-flex-vnet-nic01-defaultIpConfiguration'
) afterchange
from
azure.network.network_interfaces
where subscription_id = '0123456789'
and resource_group_name = 'vm-rg'
and network_interface_name = 'vm1450_z1'