How do I update JSON file using PowerShell How do I update JSON file using PowerShell powershell powershell

How do I update JSON file using PowerShell


Here is a way :

$a = Get-Content 'D:\temp\mytest.json' -raw | ConvertFrom-Json$a.update | % {if($_.name -eq 'test1'){$_.version=3.0}}$a | ConvertTo-Json -depth 32| set-content 'D:\temp\mytestBis.json'

According to @FLGMwt and @mikemaccana I improve the ConvertTo-Json with -depth 32 because the default depth value is 2 and for object deeper than 2 you will receive class informations in spite of objects.


I have also faced the same kind of issue. I was looking to change the records of the below JSON file

{"SQS_QUEUE_URL":  "https://que-url.com/server1","SQS_EVENTS_QUEUE_URL":  "https://events-server.com/server1/development_events","REGION":  "region1","BUCKET":  "test-bucket","AE_WORK_PATH":  "C:\\workpath\\path1","ENV":  "env"}

Finally, I managed to find the easiest way to generate a JSON file from Powershell.

$json = Get-Content "c:\users\bharat.gadade\desktop\test.json" | ConvertFrom-Json $json.SQS_QUEUE_URL = "https://que-url.com/server2"$json.SQS_EVENTS_QUEUE_URL = "https://events-server.com/Server2/development_events"$json.REGION = "region1 "$json.BUCKET = "test-bucket"$json.AE_WORK_PATH = "C:\workpath\path1"$json.ENV = "env"$json | ConvertTo-Json | Out-File "c:\users\bharat.gadade\desktop\test.json"