38

Getting this very strange error when I am trying to load my ipython notebook. Never had it before, and I cannot to my recollection, remember having done anything silly with ipython:

Unreadable Notebook: /path/to/notebooks/results.ipynb NotJSONError('Notebook does not appear to be JSON: u\'{\\n "cells": [\\n  {\\n   "cell_type": "...',)

which is followed by

400 GET /api/contents/results.ipynb?type=notebook&_=1440010858974 (127.0.0.1) 36.17ms referer=http://localhost:8888/notebooks/results.ipynb
admdrew
  • 3,790
  • 4
  • 27
  • 39
Astrid
  • 1,846
  • 4
  • 26
  • 48
  • For me the issue was actually a python comment. But like @crypdick suggests, using an online JSON validator will show you where the issue is very quickly. – jtb Sep 23 '21 at 14:34

21 Answers21

27

Save yourself a headache. Open your .ipynb in any online JSON validator and it will tell you which lines have issues. I used this one.

crypdick
  • 16,152
  • 7
  • 51
  • 74
  • Hi i got this error with an empty file that i created by running `vim xxx`, so i don't have anything to validate... Does it mean the empty notebook also needs something like a JSON-format? – wawawa Oct 27 '22 at 10:08
  • 1
    @wawawa you need to init the ipynb file using jupyter – crypdick Oct 30 '22 at 12:08
19

In my case, I am using GitHub to save and share my ipython files with my teammate. When there is a conflict in the code, I had to delete those lines indicating the changes in the conflicting code such as:

>>>>>>>>head
=============

and It works for me.

Maged Saeed
  • 1,784
  • 2
  • 16
  • 35
  • I think I have the same problem, but I couldn't see the line in Atom. Which IDE did you use to see the line? – Cloud Cho Aug 25 '20 at 07:08
  • 1
    same thing happened, could you elaborate a bit more? – Prasanta Bandyopadhyay Oct 28 '21 at 04:36
  • This will only happen if there is a merge conflict and you did not resolve the merge. Just do a global search for >>>>> and you will find the conflicts. You can resolve them by keeping only the needed parts and removing the rest including these special keywords. Things will then be resolved, hopefully. – Maged Saeed Oct 28 '21 at 11:51
  • As @crypdick 's comment, use the online JSON validator to see where are the conflicts, and then edit them out in any text editor/IDE. – marcogemaque Feb 01 '22 at 13:11
14

This happened to me as well. I opened my data.ipynb file using notepad and found out it was blank.

I managed to recover my file by going into the hidden ipynb_checkpoints folder and copying data_checkpoint.ipynb out into my working directory.

In my Mac OS terminal

cd .ipynb_checkpoints
cp data-checkpoint.ipynb \..

Thankfully the codes were preserved. Hope this helps!

runawaykid
  • 1,391
  • 1
  • 15
  • 20
11

I just had the same issue after upgrading from IPython 0.13 (ish) to Jupyter 4.

The problem in my case were a few rogue trailing commas in the JSON, for example the comma following "outputs" in:

...
 "language": "python",
 "metadata": {},
 "outputs": [],
},

After removing the commas, Jupyter/IPython could again read the notebook (and upgraded it to version 4). I hope this helps.

cr333
  • 1,555
  • 1
  • 15
  • 16
  • Hi, only comma after each dictionary? – haneulkim Apr 26 '19 at 01:11
  • 3
    An easy way to check errors in json structure is open the jupyter notebook in vs code and click in "Accept changes" for all highlighted errors. Save and then open the nb again as usual – Lucas Oct 23 '20 at 18:17
  • The comment of @Lucas avoids using another cookies website :). [Someone has also added an answer with this](https://stackoverflow.com/a/65832564/11154841). – questionto42 Sep 06 '21 at 17:38
7

The easiest way to recover corrupted Jupyter notebook files, whether it contains text or not (size = 0KB), is to go to the project folder and display the hidden files. Once the hidden files are displayed, you will see a folder named '.ipynb_checkpoints'. Simply open this folder and take the file you want!

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
user11173578
  • 71
  • 1
  • 1
6

Jupyter autosaves in a specific way. It means You have accidentally closed the notebook before properly saving it.

You need to look for three things -

  • Search for <<<<<<< and delete those lines.
  • Search for ====== and replace those lines with ,.
  • Search for >>>>>>> and delete those lines.

It will work fine after this.

Amit Ghosh
  • 1,500
  • 13
  • 18
5

Visual studio code procedure

This is my procedure that usually avoids me groping in the dark.

  1. I installed a json parser validator like this one.
  2. Open the file and save a copy as .json file.
  3. Open the json and look for the errors.
  4. Save it back to the .ipynb extension.

Usually, I manage to fix the errors quickly.

G M
  • 20,759
  • 10
  • 81
  • 84
3

this can be changed to reformat your ipynb file to readable in jupyter notebook. check your other ipynb files(open in notepad) which are working fine with your jupyter notebook, check and compare at the end of the files in notepad. there you can reformat the file which is not working.

2

I had this issue from accidentally saving as .txt from github and solved by deleting .txt (leaving .ipynb instead of .ipynb.txt when downloading)

jjjshade
  • 101
  • 1
  • 6
2

Yes, the best solution for me was I saved my notebook in HTML format, then opened it in Notepad ++ , delete the long repeated lines of output which were causing my notebook to grow to 45MB, once that cleared, Saved the file back into (.ipynb) format , and was able to opened it with no JSON error. Hope that worked for others as well!

1

I encountered an issue with a seemingly corrupted Jupyter notebook (ipynb) file, which may have been caused by editing it externally using a text editor. None of the solutions above would work for me, mainly because I don’t have Visual Studio on the Linux-based server I use at work and I don’t have time to get familiar with it. However, I finally found a solution that worked for me: In JupyterLab, try to open the file with the “JSON” editor instead of the “Notebook” editor. While it won’t succeed in opening the file, it will readily point out any issues with the file’s formatting. In my case, the issue was a single comma in “\n”,] in one of the 2219 lines of my file. Once I corrected this, I was able to open my file as a notebook again.

FabB
  • 11
  • 1
0

Got this error after conflicts while pushing my code to Github. The code present on the repo was old, and my changes were stashed. Notebook wasn't opening in either Jupyter and github repo. Following above comments, I searched for the part in my code which was giving JSON error,i.e. '<<<<<<<<<<<', '=======' and '>>>>>>>>>>' characters using an online json parser. Then I opened my .ipynb notebook in notepad++ and manually replaced these characters with blank string ''. After this, the notebook opened on my local Jupyter, and I also pushed the changes to Github.

learner
  • 43
  • 7
0

I have changed by ipynb file encoding from UTF-8-BOM to UTF-8, and then it worked.

watsn zhein
  • 81
  • 1
  • 2
0

My native language is not English, but because this problem helped me a part, I came to feedback my solution. The following is translated with translation software: Fundamentally, the file format is messed up due to wrong closing. When opening, the correctness of the json format will be checked first, and an error will be returned if it is found to be wrong. The mess in my file format is not <<<<< or ====== but the lack of commas. Either way, it's best to use a piece of software to detect errors in the json syntax, and then manually fix it yourself. The json website detection provided by the highest praise is available, but the detection errors are not complete, and may need to be detected-modified-detected-modified. Also use vscode to open the file, vscode will prompt the location of the json syntax error, which is also incomplete and needs to be checked and modified multiple times.

The error location provided is more difficult to find. I use nodepad++, and the lower right corner can display how many characters are selected (standard, including line breaks). Then select from the first character until the destination position. Although it's a bit stupid, the main reason is that I didn't find the relevant positioning method.

IEluan
  • 1
0

Clear all outputs. Then copy the notebook.

Garf
  • 75
  • 1
  • 12
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 06 '22 at 19:37
0

If you use Jupyter-Notebook in VS code, just save it in VS code, close the file and try to open it again by accessing the browser.

0

on ubuntu 20.04, I have file String.ipynb. I had same problem because I coded ơ [ echo 'hello' >> String.ipynb ]. deleting 'hello' in String.ipynb -> I could open my notebook like normal. how did I delete? [ nano String.ipynb ] * move to last line (hello) * -> delete it. I hope my answer help you :D

LeVi
  • 1
0

you will see this error may be because, you were getting merge conflict in .ipynb file. because of that git adds >>>>>>>> HEAD thing in .ipynb file which makes is unreadable.

To overcome this issue open .ipynb file in vim editor and then remove the incoming changes or your changes as per your use case.

vim <your-.ipynb-file-path>

To remove incoming changes remove content between these lines<<<<<<<<<< HEAD ==============. Note:- remove this line as well >>>>>>>>>>>> this line.

to remove your changes remove content between these lines ============== >>>>>>>>>>>. Note:- remove this line as well a <<<<<<<<<< HEAD

Jay Parekh
  • 61
  • 3
0

I was getting the same error while uploading my Jupyter notebook while using one of the community cloud-based servers of https://www.runpod.io/.

This problem gets solved for me by either waiting for some time (1-2 minutes) post uploading the notebook or by uploading a new one/two times and then trying to open it.

Sharing this incase someone has the same problem and reaches here.

aiish
  • 356
  • 2
  • 4
0

If you are ready for some more effort and get the file somehow, this method can help you..

  1. Get the cache history of the browser on which you open the Jupyter Notebook.

If you're a Windows User

C:\Users\user_cur\AppData\Local\Google\Chrome\User Data\Default\Cache\Cache_Data

If you're a Mac User

~/.cache/chromium/Default/Cache/
  1. Run a the grep command and give the keyword which is there in the .ipynb files

If you're a Windows User and want to search the keyword across files without the lower/upper case

finest /s /i pdf *.* 

If you're a Mac User

grep -a 'pdf scraping'
  1. In the output, you'll get the file name, you need to be too observing while finding the name.

  2. Just open that file in the Notepad++ and following things:

    1. Go to Plugins
    2. If you do not have JTool, install it via Plugins Admin
    3. Go to JTool and Format JSON.
    4. Save the file as .json format
    5. Access the json and iterate over cells and that will be a list of dictionaries.
    6. You can access the source code of each cell in the value of key 'source'
    7. Save the code in the .txt file and and export it as .py and you're good to go.
-2

I had the same issue after git merge while using VS Code and Jupyter extension.

VS Code would not open the notebook after the merge conflicts were highlighted in the notebook JSON by git (e.g. <<<<<). One way around it was to highlight the changes and accept one by one using the file viewer in the VSCode git interface.

Alternative that worked for me was to rename the file to .json so that it would open and then search for each instance of <<<<< and accept the incoming change.

bmaci
  • 1
  • 1