1

I have created below JSON array in PowerShell

$json = @(
        @{
            firstname = "abc"
            lastname = "xyz"
            email = "[email protected]"           
        }
    )

When I write json to a file list/array structure is lost $json | ConvertTo-Json | Set-Content $filepath $json | ConvertTo-Json | Out-File $filePath I tried with the above commands, file looks like this

{
  "firstname": "abc",
  "lastname": "xyz",
   "email": "[email protected]"
}

I want to write json to a file without flattening the list i.e. retain the array format as shown below

[
  {
     "firstname": "abc",
      "lastname": "xyz",
       "email": "[email protected]"
  }
]

1 Answer 1

1

The pipeline will flatten/unravel the array stored in $json, so avoid piping to ConvertTo-Json at all:

ConvertTo-Json $json |Out-File $path
Sign up to request clarification or add additional context in comments.

1 Comment

This works. Thank you

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.