Saltar al contenido principal
Página traducida

Esta página se tradujo del inglés con ayuda de IA. Si hay diferencias entre ambas, la versión en inglés es la referencia.

JSON_REPLACE

Actualiza valores existentes en un objeto JSON según su ruta. JSON_REPLACE no agrega nuevas claves al documento; solo reemplaza los valores de las claves existentes.

Consulte también:
[SELECT] [JSON_EXTRACT] [JSON_SET] [JSON_REPLACE]


Sintaxis​

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

Argumentos​

json_column
La columna o expresión JSON en la que se reemplazan valores.

path
La expresión de ruta JSON que apunta a la ubicación del documento JSON donde debe realizarse el reemplazo. La ruta debe apuntar a una clave existente.

new_value
El nuevo valor de reemplazo en la ruta especificada del documento JSON.

Valor(es) devuelto(s)​

Devuelve un documento JSON modificado con el valor especificado reemplazado. Si la ruta especificada no existe, el documento JSON original se devuelve sin cambios.

Comportamiento si una clave existe
Función¿Sobrescribir si ya existe?¿Crear si no existe?
json_insert()NoSí
json_replace()SíNo
json_set()SíSí

Ejemplos​

Reemplazar un solo valor en un objeto 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"} |
|---------------------------------------|------------------------------------------|
*/

Reemplazar varios valores en un objeto 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'