How do I update JSON file using PowerShell

2019-02-01 21:24发布

I have one json file mytest.json like below I want to update values using PowerShell script

update.json

{
    "update": [
        {
            "Name": "test1",        
            "Version": "2.1"
        },
        {
            "Name": "test2",        
            "Version": "2.1"
        }   
    ]
}

I want to write a PowerShell script where if Name=="test1" I want to update Version= "3" How can i do it using parameters?

1条回答
做自己的国王
2楼-- · 2019-02-01 21:48

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  | set-content 'D:\temp\mytestBis.json'
查看更多
登录 后发表回答