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

view raw
readwritejson.ps1
hosted with ❤ by GitHub

1

I used following json file

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

view raw
sample.json
hosted with ❤ by GitHub

Done 🙂

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s