How can I fix the error "JSONDecodeError: Expecting value: ..." when loading a json file with json.load()?
Asked Answered
F

6

7

I am trying to load a json file in my Jupyter Notebook

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib as plt
import json
%matplotlib inline

with open("pud.json") as datafile:
  data = json.load(datafile)
dataframe = pd.DataFrame(data)

I am getting the following error

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Please help

Fireboat answered 2/6, 2018 at 5:57 Comment(3)
Are you sure "pud.json" is a valid JSON file? And no, you are not using pandas, you use a JSON reader.Ejaculate
sorry i uploaded the wrong codeFireboat
Your error is caused by the function json.load. What you do next does not matter.Ejaculate
A
5

If you want to load a json file, you can also use pandas.read_json() (though that will not help either if your error stems from a mere format error of your json).

pandas.read_json("pud.json")

This will load the json as a dataframe. The function usage is as shown below

pandas.read_json(path_or_buf=None, orient=None, typ='frame', dtype=True, convert_axes=True, convert_dates=True, keep_default_dates=True, numpy=False, precise_float=False, date_unit=None, encoding=None, lines=False, chunksize=None, compression='infer')

You can get more information about the parameters here http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_json.html

Alvarez answered 2/6, 2018 at 6:45 Comment(2)
If json cannot load it, pandas cannot do it either. The file must be in the right json format, which it is not. Can anyone with an error when loading the file with json drop a remark here that they could load the file with pandas instead? I do not see why this should work then, see the remark below the question. I could not load my json file with pandas either, therefore I doubt that this answer answers the question about the JSONDecodeError.Nertie
I changed and downvoted this question since it most likely does not answer the question. I am waiting for any remarks that say something else.Nertie
M
1

Another way using json!

    import pandas as pd
    import json
            
    with open('File_location.json') as f:  
      data = json.load(f)
    df=pd.DataFrame(data)
Merodach answered 9/6, 2021 at 6:7 Comment(2)
Welcome to StackOverflow and thanks for jumping in with an answer to try to help someone! I am a little confused though; perhaps you can clarify it a bit. The code you included here looks identical to the (non-working) code in the question, except the syntax error at the end of the open line, and a file name and variable name. If that is true, it does not appear to answer the question. If that is not true, please add a few words of explanation to clarify what is different and how this difference helps. In any case, include a few words of explanation along with the code in an answer.Apoenzyme
I guess this was to show that opening with with is best practice. Also, if you do not read the question, you think that this question is about loading the file at all - and not about the error that is only at the end of the question. I have changed the question so that this becomes clearer for a quick reader. I still downvoted this since it does not answer the question.Nertie
T
1

This code you are writing here is completely okay . The problem is the .json file that you are loading is not a JSON file. Kindly check that file.

Telugu answered 18/6, 2021 at 5:57 Comment(1)
This is the right answer in short. I only added an answer to show this with my own json errors.Nertie
P
0
with open('pud.json', 'r') as file:
     variable_name = json.load(file)

The json file will be loaded as python dictionary.

Poundage answered 16/3, 2021 at 9:7 Comment(1)
This does not answer the question. It shows the same code as in the question. Downvote.Nertie
N
0

I could not load the file with the json module either:

File /srv/home/seid/miniconda3/lib/python3.9/json/decoder.py:353, in JSONDecoder.raw_decode(self, s, idx)
    344 """Decode a JSON document from ``s`` (a ``str`` beginning with
    345 a JSON document) and return a 2-tuple of the Python
    346 representation and the index in ``s`` where the document ended.
   (...)
    350 
    351 """
    352 try:
--> 353     obj, end = self.scan_once(s, idx)
    354 except StopIteration as err:
    355     raise JSONDecodeError("Expecting value", s, err.value) from None

JSONDecodeError: Invalid control character at: line 2 column 914 (char 915)

Therefore, I tried it with pandas, as in one of the answers, to no avail:

File ~/.local/lib/python3.9/site-packages/pandas/io/json/_json.py:1133, in FrameParser._parse_no_numpy(self)
   1129 orient = self.orient
   1131 if orient == "columns":
   1132     self.obj = DataFrame(
-> 1133         loads(json, precise_float=self.precise_float), dtype=None
   1134     )
   1135 elif orient == "split":
   1136     decoded = {
   1137         str(k): v
   1138         for k, v in loads(json, precise_float=self.precise_float).items()
   1139     }

ValueError: Unexpected character found when decoding array value (1)

I then opened the file in VSCode as a json and checked line 2 column 914 and found that after that column, there was a tab instead of spaces.

To fix this, I regex replaced all tabs with four spaces:

enter image description here

Side remark: I had a json with many hardcoded \n linebreaks and thought that I would have to drop them as well, but these hardcoded \n do not harm, you can keep them.

I saved the file, uploaded it again (overwriting the one that was there), and ran the code with json and with pandas again. Yet, the pandas error stayed the same, only the json error was new:

JSONDecodeError: Expecting value: line 11 column 5 (char 126773)

Going to that line with Ctrl+G and 11, I found a bracket at the end of a list, and right before that in the line before, there was a wrong comma:

enter image description here

Without that comma, after uploading and overwriting again, the code ran through with both json and pandas. I only needed it as the dictionary that json loads so that I could avoid importing pandas.

Nertie answered 4/1 at 18:46 Comment(0)
S
0

Try this:

import json
import pandas as pd  
with open("pud.json", "r") as datafile:
    json_exp = json.load(datafile)
dataframe = pd.DataFrame(data)

You also need to ensure your filepath is correctly referenced, and are opening your file in read mode (r). If it still doesn't work then your json file in itself is not well parsed (missing a ',' or a '}')

Solid answered 16/8 at 15:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.