Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError when using pandas.read_json

I made a 250MB json file that should look like this:

[ {"A":"uniquevalue0", "B":[1,2,3]}, 
  {"A":"uniquevalue1", "B":[1]}, 
  {"A":"uniquevalue2", "B":[1,2,3,4]} ]

where the "B" value can be variable len >= 1. This says I have valid JSON.

I call

df = pandas.read_json('ut1.json', orient = 'records', dtype={"A":str, "B":list})

Here is the documentation. When reading into a pandas dataframe, I get the following traceback:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../pandas/io/json.py", line 198, in read_json     
    date_unit).parse()
  File "/.../pandas/io/json.py", line 266, in parse 
    self._parse_no_numpy()
  File "/.../pandas/io/json.py", line 496, in _parse_no_numpy
    loads(json, precise_float=self.precise_float), dtype=None)
ValueError: Unexpected character found when decoding 'true'

Can't think of what is going wrong. The python file that is throwing the error is not that helpful.

like image 204
ehacinom Avatar asked Dec 02 '14 02:12

ehacinom


3 Answers

I had the same error message, and I solved it by using an absolute path.

import os
basePath = os.path.dirname(os.path.abspath(__file__))
df = pandas.read_json(basePath + '/ut1.json', orient = 'records', dtype={"A":str, "B":list})

That worked for me!

like image 110
learn2day Avatar answered Oct 02 '22 09:10

learn2day


In my case, the path was wrong.

Make sure you check your current working directory, by placing this just before the pandas.read_json:

import os
print(os.getcwd())
like image 26
typhon04 Avatar answered Oct 02 '22 11:10

typhon04


After tried @learn2day's answer, I still cannot get a good result from there, but I do try the following code and everything works for me. (PS: I'm opening a JSON file where Chinese characters were UTF-8 characters appeared - Chinese characters)

pandas.read_json(open("ut1.json", "r", encoding="utf8"))

The encoding="utf8" is the key part of this code.

like image 34
imgg Avatar answered Oct 02 '22 11:10

imgg