1

I am writing a PowerShell script which passes a JSON to a C# module.

The following is the JSON:

{
"Header":  {
               "Message":  {
                               "EDept":  [
                                              "Computers",
                                              "Electronics", 
                                              "Electricals"
                                          ],
                               "EId":  "001",
                               "Emp":  "All"
                           },
               "AnotherArray":  [
                                    "E1",
                                    "E2",
                                    "E3"
                                ],
               "ID":  "JOPP"
           }
}

I basically have two Arrays/List which are : "EDept" & "AnotherArray". AnotherArray is fine but EDept is not taken as an array !!

This is PSScript:

$Header = @{}
$Header.Add("ID", "JOPP")

$message = @{}
$message.Add("Emp", "All")
$message.Add("EId", "001")
$EDept = @("Computers", "Electronics", "Electricals")
$message.Add("EDept", $EDept)
$Header.Add("Message", $message)

$anotherArray = @("E1", "E2", "E3")
$Header.Add("AnotherArray", $anotherArray)

$main= @{}
$main.Add("Header", $Header)

$mainjson = $main | ConvertTo-Json

Write-Output ($mainjson)

but the output that I get is:

{
    "Header":  {
                   "Message":  {
                                   "EDept":  "Computers Electronics     Electricals",
                                   "EId":  "001",
                                   "Emp":  "All"
                               },
                   "AnotherArray":  [
                                        "E1",
                                        "E2",
                                        "E3"
                                    ],
                   "ID":  "JOPP"
               }
}

If you see here my AnotherArray is fine but "EDept" is taken a single string rather than a array. I am defing both in the same way.

What am I doing wrong?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
user2048204
  • 729
  • 4
  • 13
  • 27

2 Answers2

2

This is a simple fix. Use the -depth parameter in your call to ConvertTo-Json:

$mainjson = $main| ConvertTo-Json -depth 99

Write-Output ($mainjson )
{
    "Header":  {
                   "Message":  {
                                   "EDept":  [
                                                 "Computers",
                                                 "Electronics",
                                                 "Electricals"
                                             ],
                                   "EId":  "001",
                                   "Emp":  "All"
                               },
                   "AnotherArray":  [
                                        "E1",
                                        "E2",
                                        "E3"
                                    ],
                   "ID":  "JOPP"
               }
}

This has bitten me many times, and is one of the few things in PowerShell that I believe they got wrong.

Christopher G. Lewis
  • 4,777
  • 1
  • 27
  • 46
1

ConvertTo-Json has Depth parameter that specifies, how many level of contained objects will be included in result JSON. In your case, it's enough to add -Depth 3 and you'll get expected result.

Alternatively you can use Newtonsoft.Json module, which has ConvertFrom-JsonNewtonsoft and ConvertTo-JsonNewtonsoft cmdlets that are wrappers for Newtonsoft.Json.

qbik
  • 5,502
  • 2
  • 27
  • 33