Hateem Ahmad Hateem Ahmad - 6 months ago 57
JSON Question

Failed to parse template: Error parsing JSON: invalid character 'ÿ' looking for beginning of value


Failed to parse template: Error parsing JSON: invalid character 'ÿ' looking for beginning of value


Im getting this error, when I use



$Parsed_json | ConvertTo-Json -Depth 999 -Compress |
Out-File $nameOfJsonFile -Force


and this:


Failed to parse template: Error parsing JSON: invalid character 'ÿ' looking for beginning of value


when using

$Parsed_json | ConvertTo-Json -Depth 999 | Out-File $nameOfJsonFile -Force


The JSON online validator approves of my JSON.

My research so far about this topic, is that the Unicode characters that prints themselves when you use
Out-File
, is creating this issue. The encoding of my JSON file is ASCII, any help regarding this issue, would be highly appreciated.

Answer

Apparently this is an encoding problem. The resolution is to create the output file using ASCII encoding (well, actually it's an ANSI encoding, but since the parameter argument is named Ascii let's stick with that for simplicity's sake), e.g. like this:

$Parsed_json | ConvertTo-Json -Depth 999 -Compress |
    Out-File $nameOfJsonFile -Encoding Ascii -Force

or like this (Set-Content uses ASCII encoding by default):

$Parsed_json | ConvertTo-Json -Depth 999 -Compress |
    Set-Content $nameOfJsonFile -Force
Comments