如何使用PowerShell脚本更新JSON文件中的节点值?

时间:2017-09-04 06:04:32

标签: json powershell deserialization powershell-v3.0

我有一个JSON文件,我需要在名称和#34; Test1"。

的位置添加多个ID。
{
    "SuppressTraceLog":  false,
    "Pattern":  "^/App/Test/\\?frame1=Detail.asp%3Frigter%3srecrd%12saemkey%3D(?\u012fid\u006e\\d+)",
    "PatternGroup":  "id",
    "Locations":  [
        {
            "Ids": [ "12", "13", "14"],
            "Name":  "TEST1",
            "UrlFormat":  "http://test.com/"
        },
        {
            "Ids": [ "42", "43" ],
            "Name":  "TEST2",
            "UrlFormat":  "http://test.com/"
        },
        {
            "Ids":  ["9", "10"],
            "Name":  "TEST3",
            "UrlFormat":  null
        }
    ]
}

现在我通过获取JSON文件并更新其位置来向Ids节点添加更多值:

$a = Get-Content $jsonFilePath -Raw | ConvertFrom-Json
$a.Locations | where {$_.Name -eq 'Test1'} | foreach {$_.Ids = ([string]$dusIds)}
$a | ConvertTo-Json | Set-Content $jsonFilePath

但是,一旦我设置内容输出错误且ID看起来像,

"Locations":  [
    {
        "Ids":  "12, 13, 14",
        "Name":  "TEST1",
        "UrlFormat":  "http://test.com/"
    },
    {
        "Ids": "42, 43" ,
        "Name":  "TEST2",
        "UrlFormat":  "http://test.com/"
    },
    {
        "Ids":  "9, 10",
        "Name":  "TEST3",
        "UrlFormat":  null
    }
]

0 个答案:

没有答案