0

I need an JSON array string like the following: [{"a":"1","b":"2","c“:"3","d": "4"}]

If I am using the following code, I am getting {medik:[{"a":"1","b":"2","c“:"3","d": "4"}]}

$body = @{
   medik =  @(
        @{
            a = '1'
            b = '2'
            c = '3'
            d = '4'
        }
    )
}
$jsbody=$body|ConvertTo-Json -Compress;
$jsbody 

Can anyone help me? Thanks in advance!

1
  • Why do you have medik = Commented Jul 20, 2020 at 22:19

1 Answer 1

2

Try it without using the pipeline.

When using the pipeline you are passing the array one hashtable at a time to ConvertTo-Json. Since there is only one hashtable, it is seen as a single hashtable not an array of hashtables.

ConvertTo-Json @(@{a = '1'; b = '2'; c = '3'; d = '4'})

Output:

PS C:\tmp\overflow> $body = @(@{a = '1'; b = '2'; c = '3'; d = '4'})
>> ConvertTo-Json $body

[
  {
    "d": "4",
    "c": "3",
    "b": "2",
    "a": "1"
  }
]
Sign up to request clarification or add additional context in comments.

1 Comment

Not a problem. If you feel this is the correct answer, please remember to mark it as accepted.

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.