loading json file using python - python

I am trying to load json file availalble here : https://gist.githubusercontent.com/anonymous/e5ef9cb96acb98e1f813d5166d472c70/raw/eabf219c51ace122ad82b7037bbf93d347fb4a9b/data.json
with open('data.json') as data_file:
data = json.load(data_file)
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/home/karimk/python/lib/python2.7/json/__init__.py", line 291, in load
**kw)
File "/home/karimk/python/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/home/karimk/python/lib/python2.7/json/decoder.py", line 367, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 3 column 1 - line 6 column 740 (char 2826 - 16384)
What wrong am I doing here?

What wrong : json is not valid
Solution : update data to be a valid json, by example remove end of line inside string item.

Related

JSON decode error while loading of json files

The json module is not loading the json file. I have provided the correct path of the json file and i am just loading the file and trying to print it however it just is showing this error and i am not able to find a way around.
import json
f = open('test.json', 'r')
json.load(f)
f.close()
The error output is :
Traceback (most recent call last):
File "C:/Users/DELL/PycharmProjects/helloworld/Data_project/Sort_user.py", line 10, in <module>
json.load(f)
File "C:\Program Files\Python37\lib\json\__init__.py", line 296, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Program Files\Python37\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Program Files\Python37\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Program Files\Python37\lib\json\decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
The file starts with { and has '' for values.It has many values and large in size.
Dummy type:
{'abc': 'abc', 'abc': 2, 'abc': 123123, 'abc': 21, 'abc': 'abc', 'abc': 'abc'}
like this many more rows
json.load expects double quotes for property names e.g.:
[{"name":"John Doe","value":1},{"name":"John Snow","value":2}]
Also, ensure that any boolean values (TRUE, FALSE) are in lower case (true, false)
You should check following too:
Expecting double quotes - 1
Expecting double quotes - 2
and importantly this : single-vs-double-quotes-in-json

JSON writing to a file error, Extra data: line 1 column 2347 (char 2346)

import json
import requests
response = requests.get('SOME_LINK_THAT_IVE_REMOVED')
try:
response.raise_for_status()
except requests.exceptions.HTTPError:
pass
#print (response.text)
with open('stats.json', 'w') as output:
json.dump(response.json(), output, indent = 4)
what it printed:
{"sessionid":"F9269D5D-2B5C-432B-B8BC-34F87F790619","game_clock_display":"02:12.84","game_clock":132.8432,"game_status":"playing","possession":[0,2],"teams":[{"players":[{"name":"Nooth","playerid":2,"position":[-8.6900005,-0.28800002,-8.6470003],"stats":{"possession_time":22.589041,"points":0,"saves":0,"goals":0,"stuns":3,"passes":0,"catches":0,"steals":0,"blocks":0,"interceptions":0,"assists":0,"shots_taken":0},"userid":658915067565875,"possession":false},{"name":"erikmelkumyan","playerid":3,"position":[-0.72400004,1.7060001,-28.595001],"stats":{"possession_time":9.5638027,"points":0,"saves":0,"goals":0,"stuns":3,"passes":0,"catches":0,"steals":0,"blocks":0,"interceptions":0,"assists":0,"shots_taken":1},"userid":2126518170756015,"possession":false},{"name":"Sandman187_","playerid":4,"position":[-2.3990002,2.3380001,-26.783001],"stats":{"possession_time":27.565685,"points":0,"saves":1,"goals":0,"stuns":4,"passes":0,"catches":0,"steals":0,"blocks":0,"interceptions":0,"assists":0,"shots_taken":1},"userid":1611289978936588,"possession":true}],"team":"BLUE
TEAM","possession":true,"stats":{"points":0,"possession_time":59.718529,"interceptions":0,"blocks":0,"steals":0,"catches":0,"passes":0,"saves":1,"goals":0,"stuns":10,"assists":0,"shots_taken":2}},{"players":[{"name":"MooneyWhy","playerid":0,"position":[-4.539,1.399,-13.481001],"stats":{"possession_time":14.364853,"points":2,"saves":1,"goals":0,"stuns":7,"passes":0,"catches":0,"steals":1,"blocks":0,"interceptions":0,"assists":0,"shots_taken":1},"userid":1265147863612788,"possession":false},{"name":"b-love","playerid":1,"position":[-11.484,2.072,0.70500004],"stats":{"possession_time":50.680099,"points":6,"saves":1,"goals":0,"stuns":1,"passes":0,"catches":0,"steals":0,"blocks":0,"interceptions":0,"assists":0,"shots_taken":1},"userid":1457786340976218,"possession":false},{"name":"onikaze","playerid":5,"position":[-7.6980004,1.268,-11.036],"stats":{"possession_time":17.629295,"points":0,"saves":0,"goals":0,"stuns":6,"passes":0,"catches":0,"steals":0,"blocks":0,"interceptions":0,"assists":0,"shots_taken":2},"userid":1636331273057819,"possession":false}],"team":"ORANGE
TEAM","possession":false,"stats":{"points":8,"possession_time":82.674248,"interceptions":0,"blocks":0,"steals":1,"catches":0,"passes":0,"saves":2,"goals":0,"stuns":14,"assists":0,"shots_taken":4}}]}
Traceback (most recent call last):
File "C:\Users\Kai\Desktop\Python\testv4.py", line 19, in <module>
json.dump(response.json(), output, indent = 4)
File "C:\Users\Kai\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\models.py",
line 897, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\Kai\AppData\Local\Programs\Python\Python37-32\lib\json__init__.py",
line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\Kai\AppData\Local\Programs\Python\Python37-32\lib\json\decoder.py",
line 340, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 2347 (char 2346)
Any help? Seems to create invalid JSON file in the text file.
Creates a NULL value on the end of the JSON files when viewed in NotePad++

ValueError: Extra data: Importing multiple JSON frame from JSON file in python

I am trying to import multiple JSON frames stored in JSON file to python.
My code is:
import json
import array
with open("J1.json") as J:
j_Data = json.load(J)
print j_Data
Error :
Traceback (most recent call last):
File "/home/abhi/Desktop/CSS HTML/Python Mongo/JSONtoMongoDB.py", line 9, in <module>
j_Data = json.load(J)
File "/usr/lib/python2.7/json/__init__.py", line 290, in load
**kw)
File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 369, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 1 column 1113 - line 1 column 2225 (char 1112 - 2224)
My JSON file data is as follows:
If you can make array of your JSON frames in JSON file:
[{"yphthd": "123.32"} , {"yphthd": "123.32"}, ... {"yphthd": "123.32"}]
and then load it:
with open("J1.json") as J:
j_Data = json.load(J)
print j_Data

Issue in reading JSON file in python

>>> import json
>>> d2 = json.loads(open("t.json").read())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/json/__init__.py", line 307, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python2.6/json/decoder.py", line 319, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python2.6/json/decoder.py", line 336, in raw_decode
obj, end = self._scanner.iterscan(s, **kw).next()
File "/usr/lib64/python2.6/json/scanner.py", line 55, in iterscan
rval, next_pos = action(m, context)
File "/usr/lib64/python2.6/json/decoder.py", line 185, in JSONObject
raise ValueError(errmsg("Expecting object", s, end))
ValueError: Expecting object: line 1 column 11 (char 11)
[ RHEL - ~/testing ]$ cat t.json
{"us": u"OFF", "val": u"5"}
Here is what I have in my JSON file and when I try to read it using open and json.load and json.loads it fails.
After using json.load
>>> import json
>>> d2 = json.load(open("t.json"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/json/__init__.py", line 267, in load
parse_constant=parse_constant, **kw)
File "/usr/lib64/python2.6/json/__init__.py", line 307, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python2.6/json/decoder.py", line 319, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python2.6/json/decoder.py", line 336, in raw_decode
obj, end = self._scanner.iterscan(s, **kw).next()
File "/usr/lib64/python2.6/json/scanner.py", line 55, in iterscan
rval, next_pos = action(m, context)
File "/usr/lib64/python2.6/json/decoder.py", line 185, in JSONObject
raise ValueError(errmsg("Expecting object", s, end))
ValueError: Expecting object: line 1 column 11 (char 11)
>>>
You are using the wrong function. Use json.load() (no s!) to load data from an open file object:
d2 = json.load(open("t.json"))
The json.loads() function expects you to pass in a string, not a file object. You'd have to read your file in that case, returning the read data:
d2 = json.loads(open("t.json").read())
Next, you have invalid JSON in that file:
{"us": u"OFF", "val": u"5"}
# ^ ^
JSON is not Python; those u prefixes are not supported nor needed. You'll need to remove those from the file before it'll load.
If you have an API producing that format, it is not giving you JSON. It could be that it is producing a (strange form of) Python syntax instead; Python itself would produce {'us': u'OFF', 'val': u'5'} (single quotes). You can have Python interpret that as Python literals with ast.literal_eval():
import ast
with open('t.json') as fileobj:
d2 = ast.literal_eval(fileobj.read())
but it could be that the format is broken in other ways we cannot determine from a single isolated sample. It could be using true and false for boolean values, like in JSON, for example.
Better to have the API fixed rather that try and work around this broken-ness.
You are using the json.loads method. More documentation here. This method is used for string arguments only. Luckily, there is a similarly named json.load method documented here. This one can be used directly on a file object.
d2 = json.load(open("t.json"))
Your issue is that the JSON is not valid.
It looks like it is a python dictionnary. u'string' is a python 2 unicode string.
If you remove the u from your strings, it works fine.
>>> import json
>>> json.load(open('i.json'))
{u'val': u'5', u'us': u'OFF'}
Here is the json file:
$ cat i.json
{"us": "OFF", "val": "5"}

Python read multiline JSON

I have been trying to use JSON to store settings for a program. I can't seem to get Python 2.6 's JSON Decoder to decode multi-line JSON strings...
Here is example input:
.settings file:
"""
{\
'user':'username',\
'password':'passwd',\
}\
"""
I have tried a couple other syntaxes for this file, which I will specify below (with the traceback they cause).
My python code for reading the file in is
import json
settings_text = open(".settings", "r").read()
settings = json.loads(settings_text)
The Traceback for this is:
Traceback (most recent call last):
File "json_test.py", line 4, in <module>
print json.loads(text)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/__init__.py", line 307, in loads
return _default_decoder.decode(s)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 322, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 1 column 2 - line 7 column 1 (char 2 - 41)
I assume the "Extra data" is the triple-quote.
Here are the other syntaxes I have tried for the .settings file, with their respective Tracebacks:
"{\
'user':'username',\
'pass':'passwd'\
}"
Traceback (most recent call last):
File "json_test.py", line 4, in <module>
print json.loads(text)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/__init__.py", line 307, in loads
return _default_decoder.decode(s)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 319, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 336, in raw_decode
obj, end = self._scanner.iterscan(s, **kw).next()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/scanner.py", line 55, in iterscan
rval, next_pos = action(m, context)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 155, in JSONString
return scanstring(match.string, match.end(), encoding, strict)
ValueError: Invalid \escape: line 1 column 2 (char 2)
'{\
"user":"username",\
"pass":"passwd",\
}'
Traceback (most recent call last):
File "json_test.py", line 4, in <module>
print json.loads(text)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/__init__.py", line 307, in loads
return _default_decoder.decode(s)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 319, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 338, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
If I put the settings all on one line, it decodes fine.
Get rid of all of the backslashes and all of the "Pythonic" quoting in the settings file. Works fine if the file is just:
{
"user":"username",
"password":"passwd"
}
Note also that JSON strings are quoted with double quotes, not single quotes. See JSON spec here:
http://www.json.org/
>>> s = """
{
"user":"username",
"password":"passwd"
}
"""
>>> json.loads(s)
{'password': 'passwd', 'user': 'username'}
json doesn't consider \ to be a line-continuation character.
Try to use eval(s)
s="""
{\
'user':'username',\
'password':'passwd',\
\
"""
ss=eval(q)
qq
{'password': 'passwd', 'user': 'username'}
type(qq)
dict

Categories