0

I have a JSON file, where I need to add multiple IDs to locations for name "Test1".

{
    "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
        }
    ]
}

Right now I am adding more values to the Ids node by getting the JSON file and updating its locations as below:

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

However, once I set the content the output is wrong and the IDs looks like,

"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
    }
]
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
OseeAliz
  • 69
  • 3
  • 10
  • What is `$dusIds`? At a wild guess, try `foreach{$_.Ids = @($dusIds)` – arco444 Sep 04 '17 at 06:43
  • $dusIds is array which contain list of id's(numbers) I am setting it as $dusIds+= $_."Dus ID". – OseeAliz Sep 04 '17 at 06:50
  • tried adding @($dusIds) before id's but doesn't work. Any other idea? – OseeAliz Sep 04 '17 at 09:52
  • @arco444 Also tried adding convertToJson at the end of the loop as: @($pondusIds)| ConvertTo-Json and now the id's are generating like: "Ids": "[\r\n \"2666591\",\r\n \"2666592\",\r\n]" – OseeAliz Sep 04 '17 at 10:01
  • For those of you who run into this problem. I managed to solve this by adding a parameter known as -Depth. When converting to json, just set -depth to 7 as :$a | ConvertTo-Json -depth 7 | Set-Content $jsonFilePath. and it works. – OseeAliz Sep 04 '17 at 11:03

0 Answers0