如何使用PowerShell更新JSON文件 [英] How do I update JSON file using PowerShell
本文介绍了如何使用PowerShell更新JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个json文件mytest.json
,如下所示,我想使用PowerShell script
I have one json file mytest.json
like below I want to update values using PowerShell script
update.json
update.json
{
"update": [
{
"Name": "test1",
"Version": "2.1"
},
{
"Name": "test2",
"Version": "2.1"
}
]
}
我想写一个PowerShell脚本,其中if Name=="test1" I want to update Version= "3"
我该如何使用参数呢?
I want to write a PowerShell script where if Name=="test1" I want to update Version= "3"
How can i do it using parameters?
推荐答案
这是一种方法:
$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'
根据@FLGMwt和@mikemaccana,我用-depth 32
改进了ConvertTo-Json
,因为默认深度值为2,对于深度大于2的对象,尽管有对象,您仍会收到类信息.
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.
这篇关于如何使用PowerShell更新JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文