0

I am trying to build a Powershell array containing a single hashtable.

$params = @{
        "name" = "bob"
        "age" = "30"
        }


$params | ConvertTo-Json

current output:

{
"age":  "30",
"name":  "bob"
}

Desired output:

 [
    {
    "age":  "30",
    "name":  "bob"
    }
]

1 Answer 1

4

Try it this way:

$params = @{
        "name" = "bob"
        "age" = "30"
        }


 ConvertTo-Json @($params)

The @() syntax makes it an array.

You have to give the ConvertTo-Json cmdlet the input as a parameter, because the pipeline will automatically "unroll" the array and you'll be right back where you started.

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

1 Comment

thats it! I did keep ending up back right where I started! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.