在Powershell中查找并替换JSON中具有不同值的所有实例



我有一个大JSON,它的值需要用其他值替换。基本上,我正在尝试在powershell中查找和替换所有内容。我想删除我们看到的Rock-to-Rocky

JSON-

{
"Customer": [
{
"id": "123"
}
],
"Nationality": [
{
"name": "US",
"id": "456"
}
],
"address": [
{
"$type": "Home",
"name": "Houston",
"streets": [
{
"name": "Union",
"postalCode": "10",
}
]
},
{
"$type": "Office",
"name": "Hawai",
"streets": [
{
"name": "Rock",
"postalCode": "11",
}
]
},
{
"$type": "Office1",
"name": "Hawaii",
"streets": [
{
"name": "Rock 678",
"postalCode": "11",
}
]
},
{
"$type": "Office2",
"name": "Hawai2",
"streets": [
{
"name": "Rock",
"postalCode": "11",
}
]
}

]
}

我正在将JSON更改为对象,但不确定如何查找和替换所有JSON。我想删除我们看到的Rock-to-Rocky

代码

$FileContent = Get-Content -Path "Test.json" -Raw | ConvertFrom-Json
$FileContent.address.streets.name = 'Rocky' // I want to do for all the instances.
$FileContent | ConvertTo-Json -Depth 100 | Out-File "Test.json" -Force

如何使用Powershell替换文件中的字符串值

  • 将"Rock"替换为"Rocky";

  • 设置文件中的内容。

    (Get-Content -path C:UsersUsernameFolderFile.json -Raw) -replace 'Rock' , 'Rocky' | Set-Content -path C:UsersUsernameFolderFile.json;
    

相关内容

  • 没有找到相关文章

最新更新