2

I am trying to manipulate JSON object that has an array nested. The following PowerShell commands:

@{testArray=@(1,2)} | ConvertTo-Json -Compress
@{testArray=@(@{prop1=1})} | ConvertTo-Json -Compress
@{testArray=@(@{prop1=@(1,2)})} | ConvertTo-Json -Compress

Produce the following output:

{"testArray":[1,2]}
{"testArray":[{"prop1":1}]}
{"testArray":[{"prop1":"1 2"}]}

The first two do what I would expect but the last one doesn't. I would expect output:

{"testArray":[{"prop1":[1,2]}]}

What am I missing here? Using PowerShell 5.1

RVid
  • 1,207
  • 1
  • 14
  • 31

1 Answers1

3

You would need to specify the depth for ConvertTo-Json

 @{testArray=@(@{prop1=@(1,2)})} | ConvertTo-Json -Depth 3 -Compress

Will return

{"testArray":[{"prop1":[1,2]}]}
Gridonyx
  • 181
  • 8