Tengo un archivo Json con varios comentarios y quiero reemplazar un valor en él.
Intenté lo siguiente y me da un archivo json sin comentarios. Pero no entiendo cómo cambiar el valor y guardarlo con comentarios. ¿Es esto posible porque estamos reemplazando todos los comentarios con líneas vacías?
$json = Get-Content $jsonfile -Raw | ConvertFrom-Json $configfile = $json -replace '(?m)(?<=^([^"]|"[^"]*")*)//.*' -replace '(?ms)/\*.*?\*/'Mi config.json es el siguiente y quiero cambiar el valor de "versión" a 10 a través de un script de PowerShell y luego guardarlo con los comentarios intactos.
{ "FramewokSettings": { "Name": "VX", "Version": "8", // The value here is not constant. It can be something like v1.4.56.456 also "GitVersion": "v5", "DatabaseVersion": "7", // Doing xyz "CounterVersion": "2" // Doing ABC. // Start } }Utilizar
(Get-Content test.txt) -replace '^(\s*"Version"\s*:\s*")[^"]*', '${1}10' | Set-Content test.txtVer prueba .
EXPLICACIÓN
-------------------------------------------------------------------------------- ^ the beginning of the string -------------------------------------------------------------------------------- ( group and capture to $1: -------------------------------------------------------------------------------- \s* whitespace (0 or more times (matching the most amount possible)) -------------------------------------------------------------------------------- "Version" '"Version"' -------------------------------------------------------------------------------- \s* whitespace (0 or more times (matching the most amount possible)) -------------------------------------------------------------------------------- : ':' -------------------------------------------------------------------------------- \s* whitespace (0 or more times (matching the most amount possible)) -------------------------------------------------------------------------------- " '"' -------------------------------------------------------------------------------- ) end of $1 -------------------------------------------------------------------------------- [^"]* any character except: '"' (0 or more times (matching the most amount possible))