Unexpected token # in JSON at position 0 when opening ipynb file in vscode

Viewed 20449

I have a ipynb file (a jupyter notebook) which I am opening in vscode with python extension. I receive the error in the title

Unexpected token # in JSON at position 0

which I dont understand at all, since the file is supposed to be interpreted as a python file.

I can change the extension to .py and its opened fine by vscode, but I dont have the decorators to run/debug cells like define here (https://code.visualstudio.com/docs/python/jupyter-support-py).

I know the file is correct because I have use it in another vscode installation in another computer and works fine.

I have no idea what might be misconfigured in my environment... Any tops would be really helpful.

Here is the actual python code I have that its producing the mentioned error my actual environment.

issue.ipynb

# %%
import world as w
import world_eg as weg
import world_case1 as wc1
import simulator_static as simulation
import numpy as np
from scipy.optimize import minimize
import matplotlib.pyplot as plt```

From the error, I understand that is parsing the file as a JSON file and the first line, which contains the #, fails. 
6 Answers

I had a similar problem and when I opened the notebook with an editor I saw I had merge markings that git had put into the file. e.g.

<<<<<<< HEAD
...
=======
...
>>>>>>> ...

Cleaning up these, allowed jupyter to parse the file and run the notebook.

This happens when you make a request to the server and parse the response as JSON, but it’s not JSON. JSON should start with a valid JSON value – an object, array, string, number, or false/true/null. The root cause is that the server returned HTML or some other non-JSON string.

I've tried your code in my project and nothing wrong. everything looks fine. Check the Jupyter Server network, try to restart vscode and recreate a new juypter file, and see if the problem goes away.

enter image description here

[edit]

like the above screenshot shows, type # %% will add a new cell. Equally, when you open a .ipynb file, if python extension distinguishes the # %%, button run cell | debug cell will be displayed automatically for you to do further test.

enter image description here

you can copy your code without # %% to a new created blank juypter file, then click the button export as and select Python Script to got button Run Cell | Debug Cell .

OR reinstall python extension and try again.

I had the same issue and for me that problem was solved simply by deleting the underscore (_) from the file name. I don't know why but it works.

.ipynb files aren't actually Python source code files - they're encoded as JSON files. If you create a new notebook, then rename the file extension or open it in some text editor, you'll see the structure of the underlying JSON file.

When VS Code tries to interpret your file, it's trying to parse Python source as a JSON object, which will obviously fail, and lead to the otherwise cryptic error of an unexpected token.

In other words, it's not possible to convert a Python script into a notebook just by changing the file extension. Manually copying & pasting the code around will work, or you could try googling for some tool, e.g. https://github.com/remykarem/python2jupyter

I had a similar issue when creating a new file in VS Code which I saved as .ipynb. After closing the file, I was not able to reopen and I received the same error message as above.

For me, simply closing and restart VS Code did the trick. Afterwards, the .ipynb-file opened as expected.

notepad encoding format

Resaving the document using another encoding format solved my problem. A regular.ipynb file (left image) is saved using Unix(LF) format, but the file that couldn't open was saved using UTF (right image)

Related