Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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. 
like image 487
jjcasmar Avatar asked Aug 03 '20 23:08

jjcasmar


People also ask

How do I fix unexpected token error?

As you write your JavaScript application, the unexpected token error always occurs because JavaScript expected a specific syntax that's not fulfilled by your current code. You can generally fix the error by removing or adding a specific JavaScript language symbol to your code.

What does uncaught SyntaxError unexpected token '<' mean?

The error Uncaught SyntaxError: Unexpected token < is most commonly caused by your site's code referring to an asset that is no longer available. Most commonly, this is due to a filename change in assets generated during your build.

How do I fix unexpected token in Unix?

The error message syntax error near unexpected token `(' occurs in a Unix-type environment, Cygwin, and in the command-line interface in Windows. This error will most probably be triggered when you try to run a shell script which was edited or created in older DOS/Windows or Mac systems.

How do I fix unexpected token in JSON error?

The "Unexpected token u in JSON at position 0" error occurs when we pass an undefined value to the JSON. parse or $. parseJSON methods. To solve the error, inspect the value you're trying to parse and make sure it's a valid JSON string before parsing it.


Video Answer


2 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.

like image 75
John M. Conroy Avatar answered Nov 04 '22 22:11

John M. Conroy


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.

like image 42
Molly Wang-MSFT Avatar answered Nov 04 '22 20:11

Molly Wang-MSFT