0

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.

Ansgar Wiechers
  • 175,025
  • 22
  • 204
  • 278
Hateem Ahmad
  • 3
  • 1
  • 5
  • When/where exactly are you getting this error? When converting `$Parsed_json`? When writing the file? Or when reading the file that you wrote? In case of the latter: show the code that actually throws the error. And did you try `Set-Content` (which defaults to ASCII output) or `Out-File -Encoding Ascii` (to override the default UTF-8 encoding)? – Ansgar Wiechers May 18 '16 at 14:46
  • Using Out-file with -3ncoding parameter solves the issue. Thanks for your time. – Hateem Ahmad May 23 '16 at 10:04

1 Answers1

1

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
Ansgar Wiechers
  • 175,025
  • 22
  • 204
  • 278