I have a json object which returns from the following PowerShell mand:
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 plains:
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": [
]
}
I have a json object which returns from the following PowerShell mand:
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 plains:
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": [
]
}
Share
Improve this question
edited Jul 23, 2021 at 12:00
Álvaro González
147k45 gold badges279 silver badges377 bronze badges
asked Jul 23, 2021 at 11:45
tautftautf
3791 gold badge5 silver badges16 bronze badges
3
- You name.json file doesn't have correct syntax. Can you post name.json content? – mk23 Commented Jul 23, 2021 at 11:48
- 2 That's probably the UTF-16 BOM that PowerShell mands tend to add. – Álvaro González Commented Jul 23, 2021 at 11:59
- @ÁlvaroGonzález How can i resolve this? Just read the file as utf-16? – tautf Commented Jul 23, 2021 at 12:02
4 Answers
Reset to default 3Your JSON file seems to have a different encoding, maybe is utf16le instead of utf8.
I replicated your scenario and found help here: Strange unicode characters when reading in file in node.js app
fs.readFile(file, 'utf16le', (err, data)...
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
pletely 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'