ConvertFrom-Json : Invalid JSON primitive:
Approach 1 : -Raw Attempted using -Raw with Get-Content so that Get-Content instead of reading each line separately and storing as array, creates object.
$JsonContent = Get-Content $TemplateParameterFileLocal -Raw | ConvertFrom-Json
Approach 2 : Out-String Attempted with Get-Content piped to | Out-String as below:
$JsonContent = Get-Content $TemplateParameterFileLocal | Out-String | ConvertFrom-Json
Review JSON with IDEFinally, I recollected the IDE notification when I had opened up the saved copy of CMS generated JSON. It had a EOF expected but if you notice the above JSON structure, it got a ',' which was causing this trouble.
I tried both -Raw and Out-String execution again, and it was working as expected.
You may need to specify the encoding type using IO.File, for example to load a file using Windows-1252:
$myObj = [IO.File]::ReadAllText($filePath, [Text.Encoding]::GetEncoding(1252)) | ConvertFrom-Json
You may also have this problem with an old version of PowerShell (5.1) if your json contains a trailing comma.
PowerShell 5.1: ❌
PowerShell Core 7.1.3: ✔
{ "Key": "Value",}
Error in 5.1:
ConvertFrom-Json : Invalid JSON primitive: .
PowerShell 5.1: ✔
{ "Key": "Value"}