first post here.
I've been using Python for a while now, but I'm stuck with a very simple case.
I just want to parse a JSON file with simplejson module: here is the code:
import simplejson
with open('myjsontest.json', 'r') as data_file:
print data_file.read()
session = simplejson.load(data_file, strict=False)
And here is the JSON file named myjsontest.json:
[
{
"Test1": 1,
"Test2": 2,
"Test3": 3,
"Test4": 4
}
]
The JSON file is in the same folder as the python file.
I got this as a result:
[
{
"Test1": 1,
"Test2": 2,
"Test3": 3,
"Test4": 4
}
]
Traceback (most recent call last):
File ".\test.py", line 8, in <module>
session = simplejson.load(data_file, strict=False)
File "C:\Users\Gordon\Anaconda2\lib\site-packages\simplejson-3.8.1-py2.7.egg\simplejson\__init__.py", line 459, in loa
d
use_decimal=use_decimal, **kw)
File "C:\Users\Gordon\Anaconda2\lib\site-packages\simplejson-3.8.1-py2.7.egg\simplejson\__init__.py", line 533, in loa
ds
return cls(encoding=encoding, **kw).decode(s)
File "C:\Users\Gordon\Anaconda2\lib\site-packages\simplejson-3.8.1-py2.7.egg\simplejson\decoder.py", line 370, in deco
de
obj, end = self.raw_decode(s)
File "C:\Users\Gordon\Anaconda2\lib\site-packages\simplejson-3.8.1-py2.7.egg\simplejson\decoder.py", line 400, in raw_
decode
return self.scan_once(s, idx=_w(s, idx).end())
File "C:\Users\Gordon\Anaconda2\lib\site-packages\simplejson-3.8.1-py2.7.egg\simplejson\scanner.py", line 127, in scan
_once
return _scan_once(string, idx)
File "C:\Users\Gordon\Anaconda2\lib\site-packages\simplejson-3.8.1-py2.7.egg\simplejson\scanner.py", line 87, in _scan
_once
raise JSONDecodeError(errmsg, string, idx)
simplejson.scanner.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I think think I may have a problem in my OS/python setup? Python 32b 2.7.11 is installed with Anaconda on a Windows7 64b.
Thanks if you can help.
Once you read a file, its stream is at the end and cannot be read from anymore. Your code should work if you remove the print data_file.read() statement, or you .seek() back to the beginning of the file afterwards.
Related
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
>>> 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 am trying to query DBpedia using SPARQLWrapper in Python (v3.3). This is my query:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?slot WHERE {
<http://dbpedia.org/resource/Week> <http://www.w3.org/2002/07/owl#sameAs> ?slot
}
It results in an error from the SPARQLWrapper package:
ValueError: Invalid \escape: line 118 column 74 (char 11126)
Code:
query = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?slot WHERE{{ {subject} {predicate} {object} }} "
query = query.format(subject=subject, predicate=predicate, object= objectfield)
self.sparql.setQuery(query)
self.sparql.setReturnFormat(JSON)
results = self.sparql.query().convert() # Error thrown at this line
Error :
Traceback (most recent call last):
File "getUriLiteralAgainstPredicate.py", line 84, in <module>
sys.exit(main())
File "getUriLiteralAgainstPredicate.py", line 61, in main
entity,predicateURI,result = p.getObject(dataAtURI,predicates, each["entity"])
File "getUriLiteralAgainstPredicate.py", line 30, in getObject
result = self.run_sparql("<"+subjectURI+">","<"+predicateURI+">","?slot")
File "getUriLiteralAgainstPredicate.py", line 24, in run_sparql
results = self.sparql.query().convert()
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/SPARQLWrapper-1.5.2-py3.3.egg/SPARQLWrapper/Wrapper.py", line 539, in convert
return self._convertJSON()
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/SPARQLWrapper-1.5.2-py3.3.egg/SPARQLWrapper/Wrapper.py", line 476, in _convertJSON
return jsonlayer.decode(self.response.read().decode("utf-8"))
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/SPARQLWrapper-1.5.2-py3.3.egg/SPARQLWrapper/jsonlayer.py", line 76, in decode
return _decode(string)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/SPARQLWrapper-1.5.2-py3.3.egg/SPARQLWrapper/jsonlayer.py", line 147, in <lambda>
_decode = lambda string, loads=json.loads: loads(string)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/json/decoder.py", line 352, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/json/decoder.py", line 368, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Invalid \escape: line 118 column 74 (char 11126)
The problem is, that dbpedia output has this line:
{ "slot": { "type": "uri", "value": "http://got.dbpedia.org/resource/\U00010345\U00010339\U0001033A\U00010349" }},
Notice literals which start with \U (capital U). This is not valid JSON and python doesn't know how to handle it. So, problem is on DBPedia side and it can't be handled on SPARQLWrapper side.
But… You can handle it yourself like this:
results = self.sparql.query()
body = results.response.read()
fixed_body = body.decode("unicode_escape")
from SPARQLWrapper.Wrapper import jsonlayer
results = jsonlayer.decode(fixed_body)
try python-cjson
so the above thing can also be tried as below
import cjson
results = self.sparql.query()
body = results.response.read()
results = cjson.decode(body)
I have a large JSON document stored in a pretty-print format to file, where the file looks like:
$ nano data.json
{
"type" : "object",
"properties" : {
"price" : {"type" : "number"},
"name" : {"type" : "string"},
},
}
The traditional ways I've found for reading such json files, such as...
with open('data.json', 'r') as handle:
data = json.load(handle)
and...
json_data=open('data.json','r')
data = json.load(json_data)
json_data.close()
and...
data = []
with open('data.json') as f:
for line in f:
data.append(json.loads(line))
and...
ss = ''
with open('data.json', 'r') as f:
for line in f:
ss += ''.join(line.strip())
data = json.loads(ss.decode("utf-8","replace"))
...seem to only work for single-string, not pretty-print formatted JSON.
How would I load JSON of this format from a file? The errors I keep getting when trying these formats are...
Traceback (most recent call last):
File "<stdin>", line 7, in <module>
File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting , delimiter: line 1 column 250 (char 250)
ValueError: Expecting , delimiter: line 9 column 13 (char 310)
For anyone else finding this in the future googling things similar to what I did when I posted this---you may think your error is in the loading, but mine as above was actually in the JSON itself, rather than the loading (as Martijn Pieters pointed out). I was copying the schema from the jsonschema python project---but this, it turned out, was not JSON, but a deceptively similar-looking python dictionary.
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