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.
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!
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())
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With