-2

I am trying to read a json file produced by a powershell script which reads another powershell script and outputs an Abstract Syntax Tree of the that script to a json file When I try to read this output json file and parse it as json using nodejs I get the following error (although the json file looks like valid json when i open it in vscode )

undefined:1
��[
^
Unexpected token � in JSON at position 0
Ansh Malik
  • 162
  • 2
  • 7
  • 1
    Could happen when files a transferred between linux and windows, Open the json file in a text editor and play with the encoding. On ANSII or UTF8 the characters should be visible, then you can delete it and save it again. What OS are you using? – Marc Oct 27 '22 at 13:14
  • "*produced by a powershell script *". How? How do you write to the file? Did you take care of any encoding? Please be more specific on how you build this file (see: [how to ask](https://stackoverflow.com/help/how-to-ask)) – iRon Oct 27 '22 at 13:44
  • "*produced by a powershell script*". How? Please [edit] your question to provide a [mcve]. Which PowerShell version? Windows PowerShell tends to write a [Byte order mark](https://en.wikipedia.org/wiki/Byte_order_mark) at position 0… – JosefZ Oct 27 '22 at 16:15

1 Answers1

0

The error is indicating some unsupported characters at the beginning of your json ��

��[
^
Unexpected ...

That is most likely the reason for this error, as it seems. There could also be other kind of problems like

  • invalid json syntax (check for quotations, trailing commas, etc.)
  • Json file content not being sent properly to the parser (ensure that you're passing the proper json content, without any kind of noise like the one shown in your error)
ranjanistic
  • 111
  • 6