errors with json.loads

john polo jpolo at mail.usf.edu
Thu Sep 21 12:18:14 EDT 2017


On 9/20/2017 5:58 PM, Bill wrote:
> Interesting problem, John.
>
> I have probably even less experience with json than you do, so I'm 
> taking this as an opportunity to learn with you.
>
> Suggestions:
>
> 1. Try your example with Python 2 rather than Python 3.
Bill,
Thanks for the reply. I wasn't sure how to get Python 2 through the cmd 
or IPython, so I went through ArcGIS, but it's mostly the same result:

 >>> file = open('books.json','r')
 >>> text = file.read()
 >>> text = json.loads(text)
Runtime error
Traceback (most recent call last):
   File "<string>", line 1, in <module>
NameError: name 'json' is not defined
 >>> import json   #whoops, forget to do that first in this go 'round.
 >>> text = json.loads(text)
Runtime error
Traceback (most recent call last):
   File "<string>", line 1, in <module>
   File "C:\Python27\ArcGIS10.4\Lib\json\__init__.py", line 338, in loads
     return _default_decoder.decode(s)
   File "C:\Python27\ArcGIS10.4\Lib\json\decoder.py", line 366, in decode
     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
   File "C:\Python27\ArcGIS10.4\Lib\json\decoder.py", line 382, in 
raw_decode
     obj, end = self.scan_once(s, idx)
ValueError: Expecting : delimiter: line 5 column 50 (char 161)
 >>>

> 2. Take your file and make it into a string literal in your program, 
> and try calling json.loads with that as an argument. 
I am not sure I follow what you meant, but this was what I did:
In [26]: file2 = open('books.txt')

In [27]: text2 = file2.read()

In [28]: text2 = json.loads(text2)
---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-28-78a8123d71bd> in <module>()
----> 1 text2 = json.loads(text2)

c:\users\jpolo\appdata\local\programs\python\python36\lib\json\__init__.py 
in lo
ads(s, encoding, cls, object_hook, parse_float, parse_int, 
parse_constant, objec
t_pairs_hook, **kw)
     352             parse_int is None and parse_float is None and
     353             parse_constant is None and object_pairs_hook is 
None and not
  kw):
--> 354         return _default_decoder.decode(s)
     355     if cls is None:
     356         cls = JSONDecoder

c:\users\jpolo\appdata\local\programs\python\python36\lib\json\decoder.py 
in dec
ode(self, s, _w)
     337
     338         """
--> 339         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
     340         end = _w(s, end).end()
     341         if end != len(s):

c:\users\jpolo\appdata\local\programs\python\python36\lib\json\decoder.py 
in raw
_decode(self, s, idx)
     353         """
     354         try:
--> 355             obj, end = self.scan_once(s, idx)
     356         except StopIteration as err:
     357             raise JSONDecodeError("Expecting value", s, 
err.value) from
None

JSONDecodeError: Expecting ':' delimiter: line 5 column 50 (char 161)


best,
John




More information about the Python-list mailing list