-
Notifications
You must be signed in to change notification settings - Fork 1.7k
ConvertFrom-Json and newlines #3543
Copy link
Copy link
Closed
Labels
area-utilityArea - Microsoft.PowerShell.Utility moduleArea - Microsoft.PowerShell.Utility module
Description
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-JsonGet-Content JsonFile.JSON -Raw | ConvertFrom-JsonGet-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.
- ID: 6f472e83-d469-1493-1bc8-5b736308dc97
- Version Independent ID: 28f7f22d-c3a6-ea62-c77b-672d7e16fc80
- Content: ConvertFrom-Json (Microsoft.PowerShell.Utility)
- Content Source: reference/6/Microsoft.PowerShell.Utility/ConvertFrom-Json.md
- Product: powershell
- GitHub Login: @JamesWTruher
- Microsoft Alias: jimtru
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
area-utilityArea - Microsoft.PowerShell.Utility moduleArea - Microsoft.PowerShell.Utility module