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
Related
I'm trying to load a file to a variable, but I get the error json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) in whitelist = json.load(f), what am I doing wrong?
def load_whitelist():
global whitelist
wl = "C:/Users/Administrator/Desktop/Software/whitelist.json"
if os.path.isfile(wl):
with open(wl, mode='r') as f:
whitelist = json.load(f)
f.close()
print(whitelist)
def save_whitelist():
wl = "C:/Users/Administrator/Desktop/Software/whitelist.json"
if os.path.isfile(wl):
with open(wl, mode='w') as f:
json.dump(whitelist, f, sort_keys=False)
f.close()
Full Traceback:
Traceback (most recent call last):
File "PC200.py", line 970, in <module>
load_whitelist()
File "PC200.py", line 51, in load_whitelist
whitelist = json.load(f)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x0000020022935828>
The JSON
[{"ignoresPlayerLimit": false, "name": "AndRKConnor8000","xuid":"2535435055474031"},{"ignoresPlayerLimit":false,"name":"ThePurplishGame","xuid":"2535461240132600"}]
Not really an answer but it cannot fit into a comment. With only the error message, it is impossible to know what happens. This error can be reproduced with an empty file, or with an incorrect encoding. Example simulating an empty file:
>>> import json
>>> import io
>>> fd = io.StringIO()
>>> wl = json.load(fd)
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
wl = json.load(fd)
File "D:\Program Files (x86)\Python37-32\lib\json\__init__.py", line 296, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "D:\Program Files (x86)\Python37-32\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "D:\Program Files (x86)\Python37-32\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "D:\Program Files (x86)\Python37-32\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Example simulating an UTF16 encoded file read as Latin1:
>>> t = '''[{"ignoresPlayerLimit": false, "name": "AndRKConnor8000","xuid":"2535435055474031"},{"ignoresPlayerLimit":false,"name":"ThePurplishGame","xuid":"2535461240132600"}]'''
>>> tt = t.encode('utf16').decode('latin1')
>>> fd=io.StringIO(tt)
>>> wl = json.load(fd)
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
wl = json.load(fd)
File "D:\Program Files (x86)\Python37-32\lib\json\__init__.py", line 296, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "D:\Program Files (x86)\Python37-32\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "D:\Program Files (x86)\Python37-32\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "D:\Program Files (x86)\Python37-32\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
The only way to discriminate the problem is to read and print the content of the file:
with open(wl, mode='r') as f:
data = f.read() # read file conten
print(data) # display it as text
print([hex(ord(i)) for i in data]) # and as an hex dump
That way you will be sure of the way Python actually reads the file.
def load_whitelist():
global whitelist
wl = "C:/Users/Administrator/Desktop/Software/whitelist.json"
if os.path.isfile(wl):
with open(wl, mode='r') as f:
whitelist = json.load(f)
f.close()
def save_whitelist():
wl = "C:/Users/Administrator/Desktop/Software/whitelist.json"
print(whitelist)
with open(wl, mode='w') as f:
json.dump(whitelist, f, sort_keys=False)
f.close()
It seems like there was an error with saving the whitelist and it deleted the JSON contents, the code itself was ok (I removed the if os.path.isfile(wl): as it was unnecessary). Thank you all for trying to help!
I have some annoying elements in a JSON file that go something like:
"DateTime" : Date(-62135596800000),
"ReceivedDateTime" : Date(-62135596800000)
where serialising this using json.Load() results in an error because Date() is unrecognized.
Traceback (most recent call last):
File "json_parse.py", line 10, in <module>
data = json.load(data_file)
File "C:\Python27\lib\json\__init__.py", line 291, in load
**kw)
File "C:\Python27\lib\json\__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
so the easiest thing to do is to remove the Date() wrapper before serialising. I can then convert to proper datetime afterwards.
I can do simple things with str.replace such as:
data.replace("Date(","")
but obviously I am not removing the trailing bracket.
How might I go about doing this?
Cheers.
The more readable way would be to use re library and create regex:
import re
text = '''"DateTime" : Date(-62135596800000),
"ReceivedDateTime" : Date(-62135596800000)'''
pattern = re.compile("Date\((.+)\)")
x = pattern.findall(text)
text2 = text
for i in x:
text2 = text2.replace("Date("+i+")", i)
I wrote this code for you, it should solve the problem.
a = '''"DateTime" : Date(-62135596800000),
"ReceivedDateTime" : Date(-62135596800000)'''
while "Date(" in a: a = (a[:a.index("Date(")+len("Date(")+a[a.index("Date(")+len("Date("):].index(")")] + a[a.index("Date(")+len("Date(")+a[a.index("Date(")+len("Date("):].index(")")+1:]).replace("Date(", "", 1)
I have this configuration file
[section1]
namespace = {'pro1':'http://pro1/go','pro2':'http://pro2/go','pro3':'http://pro3/go'}
Reading the file with ConfigParser I got:
>>> from ConfigParser import ConfigParser
>>> Config = ConfigParser()
>>> Config.read("myfile.ini")
>>> name = Config.get("section1", "namespace")
>>> name
"{'pro1':'http://pro1/go','pro2':'http://pro2/go','pro3':'http://pro3/go'}"
To convert the string to dict/json:
>>> import json
>>> namespace = json.loads(name)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\json\__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)
>>>
Any ideas?
That's not valid JSON; unlike Python dicts, JSON is very strict - you always need to use double quotes.
namespace = {"pro1":"http://pro1/go","pro2":"http://pro2/go","pro3":"http://pro3/go"}
>>> 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"}
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