4

I have a Json file having multiple comments and I want to replace a value in it.

I tried the below and it gives me a json file without comments. But I don't understand how to change the value and save it back with comments. Is this even possible because we are replacing all the comments with empty lines?

$json = Get-Content $jsonfile -Raw | ConvertFrom-Json
$configfile = $json -replace '(?m)(?<=^([^"]|"[^"]*")*)//.*' -replace '(?ms)/\*.*?\*/'

My config.json is as below and I want to change the value of "version" to 10 through a powershell script and then save it back with the comments intact.

    {
        "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
         }
}
8
  • 2
    (Get-Content $jsonfile) -replace '(?<=^\s*"Version"\s*:\s*")[^"]*(?="\s*$)', '10' | Set-Content $jsonfile? Commented Oct 22, 2021 at 21:25
  • Thanks for the reply @WiktorStribiżew. I am a newbee to regex and I tried the above and get Cannot bind argument to parameter 'Path' because it is null. Commented Oct 22, 2021 at 21:35
  • 1
    @kat, before running Wiktor's code you first need to set $jsonfile = .\path\to\your\file. Commented Oct 22, 2021 at 21:42
  • You can preserve comments starting with PowerShell version 6. Are you using version 6 or greater? Commented Oct 22, 2021 at 21:51
  • 1
    I see, my suggestion did not work due to the fact there is a comma and a comment after ", the (?="\s*$) should be either (?=") or removed. Commented Oct 23, 2021 at 10:14

1 Answer 1

1

Use

(Get-Content test.txt) -replace '^(\s*"Version"\s*:\s*")[^"]*', '${1}10' | Set-Content test.txt

See proof.

EXPLANATION

--------------------------------------------------------------------------------
  ^                        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))
Sign up to request clarification or add additional context in comments.

2 Comments

If I have to replace the new value as a variable ($somenumber instead of the constant 10) How do I do that? I tried replacing 10 with $somenumber and it replaces with $somenumber instead of the value assigned to the variable `somenumber.
@kat There are mutiple ways. '${1}' + $somenumber is IMHO the simplest. "`${1}$somenumber" is also a way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.