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?

0

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.