Trouble editing a Json file.

Hi everyone thanks in advance for your responses. I’m a new bee to powershell and some of my questions might sound silly.

I’m taking a Chrome bookmark file as a sample Json file example.

$chromebookmarkfile = “$env:localappdata\Google\Chrome\User Data\Default\Bookmarks”

$jsonfile = Get-Content $chromebookmarkfile -Raw | ConvertFrom-Json

$jsonfile.Roots.Bookmark_bar.children = @($jsonfile.Roots.Bookmark_bar.children | where { $_.name -notmatch “^.search.$” })

$jsonfile | ConvertTo-Json -Depth 5 | Out-File “C:\Users\Name\Desktop\bookmarkupdated.js” -Encoding UTF8

 

After some googling, i’m able to filter the results that does not matches the word “search”, but when I try to replace instead of filtering, I’m facing hard time

$jsonfile.Roots.Bookmark_bar.children = @($jsonfile.Roots.Bookmark_bar.children | if( $.name -match “^.search.$”){$.name = “Success”})

Please correct me where i’m doing wrong. Thank you again for your time.

you are almost there. You cannot use if statement like that. Here you just need to use a Foreach-Object cmdlet and then the same if statement inside the foreach.

$jsonfile.Roots.Bookmark_bar.children = $jsonfile.Roots.Bookmark_bar.children | ForEach-Object -Process {
 if( $_.name -match "^.*search.*$"){
  $_.name = "Success"
 }
 else {
  $_
 }
}

Thanks a lot for your response. I just did a modification to initialize this as array as shown below. but still the desired value is missing in the array.

$jsonfile.Roots.Bookmark_bar.children = @($jsonfile.Roots.Bookmark_bar.children | ForEach-Object -Process {
 if( $_.name -match "^.*search.*$"){
  $_.name = "Success"
 }
 else {
  $_
 }
})

Can you show the output you get with the above code and the output you expect ?