Skip to content

ConvertFrom-Json and newlines #3543

@MizardX

Description

@MizardX

Example 3 talks about having to join the lines before converting the JSON file to a PowerShell custom object. This is not necessary, and the description is misleading.

The cmdlet first tries to convert the first item. If it succeeds, it converts the remaining items separately. But if it fails, it joins all the items into a single string, and tries again.

Even if the assumption was true, the better way would be to use the -Raw switch for Get-Content.

All of these works:

  • (Get-Content JsonFile.JSON) -join "`n" | ConvertFrom-Json
  • Get-Content JsonFile.JSON -Raw | ConvertFrom-Json
  • Get-Content JsonFile.JSON | ConvertFrom-Json
  • @( '{', '"foo":"bar"', '}' ) | ConvertFrom-Json
  • @( '{"foo":"bar"}', '{"foo":"baz"}' ) | ConvertFrom-Json

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

Labels

area-utilityArea - Microsoft.PowerShell.Utility module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions