Reading json file and update json file in powershell

There are some cases we need to update json files using powershell.

This is how I do it.


$filePath = "C:\jeevan\sample.json"
$file = ([System.IO.File]::ReadAllText($filePath) | ConvertFrom-Json)
Write-Host $file.property1
$file.property1 = Get-Random
$file | ConvertTo-Json | Out-File FilePath $filePath Encoding utf8 Force

1

I used following json file


{
"property1": 123,
"property2": "value2"
}

view raw

sample.json

hosted with ❤ by GitHub

Done 🙂