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

0

1 Answer 1

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]}]}
Sign up to request clarification or add additional context in comments.

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.