Reputation: 133
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
Upvotes: 7
Views: 66298
Reputation: 21
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 '}')
Upvotes: 0
Reputation: 645
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
Upvotes: 5
Reputation: 9590
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:
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:
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.
Upvotes: 0
Reputation: 201
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.
Upvotes: 1
Reputation: 19
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)
Upvotes: 1
Reputation: 9
with open('pud.json', 'r') as file:
variable_name = json.load(file)
The json file will be loaded as python dictionary.
Upvotes: 0