JSON.parse(): SyntaxError: Unexpected token � in JSON at position 0

Viewed 5544

I have a json object which returns from the following PowerShell command:

Get-Service -Name "name" | ConvertTo-Json -Compress > "/to/path/name.json"

If i basically open the file in vscode it seems to be correctly formatted. After i read the file with

fs.readFile(file, 'utf8', (err, data)...

and then try JSON.parse(data) im receiving the error:

undefined:1
��{
^

SyntaxError: Unexpected token � in JSON at position 0

Then i tried to do the following: data.replace(/[^\x00-\x7F]/g, "") to only have ASCII characters, which basically seems to work at least with a console.log().

But JSON.parse then complains:

undefined:1
{
 

SyntaxError: Unexpected token  in JSON at position 1

Im not sure whats the problem there. Hopefully somebody can help me with that.

This is an example json file: As i think the format is correct. Only too much spaces which are removed by the -Compress PowerShell parameter.

{
    "CanPauseAndContinue":  false,
    "CanShutdown":  false,
    "CanStop":  false,
    "DisplayName":  "OpenSSH Authentication Agent",
    "DependentServices":  [

                          ],
    "MachineName":  ".",
    "ServiceName":  "ssh-agent",
    "ServicesDependedOn":  [

                           ],
    "ServiceHandle":  {
                          "IsInvalid":  false,
                          "IsClosed":  false
                      },
    "Status":  1,
    "ServiceType":  16,
    "StartType":  4,
    "Site":  null,
    "Container":  null,
    "Name":  "ssh-agent",
    "RequiredServices":  [

                         ]
}
4 Answers

I think a problem occurred because of line breakers and tabs. I tried to parse your code and it was parsed successfully. Please, try to make your JSON data to be one line. Like here: How to remove all line breaks from a string.

Maybe its the same problem as when you're writing PHP scripts that use session objects.

In PHP, when a file is encoded with UTF8 with BOM, the PHP script that uses session completely break. To solve this, I generally open the file in Notepad++, goes to Format -> UTF8 (without BOM), and then save it again. Always works for me. This might be your case here, since these broken characters appears to be on the beginning of your file, that's exacly where BOM is.

This answer might clarify about UTF8 and BOM.

Normally the error message having some strange characters (like �) gives hint that there maybe some problem in the encoding.

A few nodejs readFile api's with different encoding

fs.readFile(file, 'utf8', function(err, data) { ... });

fs.readFile(file, 'utf16le', function(err, data) { ... }); // le - little endian

fs.readFile(file, 'ucs2', function(err, data) { ... });   // kind  of 'utf16le'
Related