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
    }
]
5
  • What is $dusIds? At a wild guess, try foreach{$_.Ids = @($dusIds) Commented Sep 4, 2017 at 6:43
  • $dusIds is array which contain list of id's(numbers) I am setting it as $dusIds+= $_."Dus ID". Commented Sep 4, 2017 at 6:50
  • tried adding @($dusIds) before id's but doesn't work. Any other idea? Commented Sep 4, 2017 at 9: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]" Commented Sep 4, 2017 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. Commented Sep 4, 2017 at 11:03

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.