1

I have data from openWeather in JSON format which gives the error

Traceback (most recent call last):
  File "testjson.py", line 7, in <module>
    data = json.load(data_file)
  File "E:\Program Files\Python27\lib\json\__init__.py", line 290, in load
    **kw)
  File "E:\Program Files\Python27\lib\json\__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "E:\Program Files\Python27\lib\json\decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "E:\Program Files\Python27\lib\json\decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

I have run the JSON data through JSONlint and it's OK. This is it

{"city":{"id":7839581,"name":"Gold Coast","coord":{"lon":153.36055,"lat":-27.97851},"country":"AU","population":0,"sys":{"population":0}},"cod":"200","message":0.0184,"cnt":40,"list":[{"dt":1488844800,"main":{"temp":297.46,"temp_min":297.12,"temp_max":297.46,"pressure":1019.12,"sea_level":1025.73,"grnd_level":1019.12,"humidity":100,"temp_kf":0.34},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"clouds":{"all":0},"wind":{"speed":5.16,"deg":203.005},"rain":{}}]}

and lastly my Python code

import json
from pprint import pprint


with open('weather.json') as data_file:    
    data = json.load(data_file)

pprint(data)

I've been messing with this for hours, chasing up leads here and anywhere else the searching takes me. Unlike a lot of more subtle errors, this just seems to be rejecting the whole lot, and I don't know why

Could anyone shed some light on this ?

3
  • 3
    That string loads using json.loads, so you might want to actually do a s = data_file.read(), then print it out and see that the contents of the weather.json is actually what you think it was. Commented Mar 11, 2017 at 1:45
  • Seems fine. Are you absolutely sure the source file is okay? Have you passed it to a JSON checker or did you just copy paste the plain text? Commented Mar 11, 2017 at 1:48
  • I checked the JSON data with several online tools and the Chrome JSON addon. I also found that the jq utility parses it just fine. But, I also tried doing an s=data_file.read then printing the string: it appears to have 3 unprintable characters at the start of it. They don't show up in a text editor though... Commented Mar 11, 2017 at 2:48

2 Answers 2

1

You need to read the data first from your file then load it using json module like this example:

import json

with open("weather.json", 'r') as f:
    # read the data
    data = f.read()
    # then load it using json.loads()
    final = json.loads(data)

print(final['city'])

Output:

{u'name': u'Gold Coast', u'country': u'AU', u'coord': {u'lat': -27.97851, u'lon': 153.36055}, u'sys': {u'population': 0}, u'id': 7839581, u'population': 0}

Edit:

You can also use json.load() to achieve this task like the example below:

import json

with open("weather.json", 'r') as f:
    final = json.load(f)

print(final['city'])

Output:

{u'name': u'Gold Coast', u'country': u'AU', u'coord': {u'lat': -27.97851, u'lon': 153.36055}, u'sys': {u'population': 0}, u'id': 7839581, u'population': 0}

Why this is working like this way ? It's simple, you may have a look at the documentation json.load() the input must have read() method:

Deserialize fp (a .read()-supporting file-like object containing a JSON document) to a Python object using this conversion table.

Otherwise within json.loads() the input must be a str or unicode instance:

Deserialize s (a str or unicode instance containing a JSON document) to a Python object using this conversion table.

PS: If both methods fails, you should take a look at your file and check if it contain a valid JSON or not.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm confused. I'm probably being slow but the second method you recommended seems to be exactly the method that was failing in the original question.
Yes. this is why i used in first place json.loads(). Otherwise if it fails again the OP must check his json file. With his current json input both methods should work fine.
Yes I have tried opening and reading the file then using loads() on the string I've read, I get the same result. When I print the string, though, i get those odd characters. using read(1) on the file, and using ord() i get 239, 187, 191. That is, creating a string from the contents of the file puts those characters at the start of it. They don't show up in vi, or any other editor I have. So, there may be somehting wrong wiht the json file, but like I said, jq parses it and all the lint checkers online think it's OK
0

OK looks like my json was corrupt. reading the first three characters gave them high-ASCII ord values, so I edited the file with a hex editor, it did indeed have 3 characters at the start. How they may have got there is an utter mystery, I have been using ascii editors the whole time. Thanks for the responses; this was really an odd one. I now have an issue that pprint's output isnt really very pretty, but I can now read the file and have a dictionary. Many thanks.

2 Comments

Yep that's exactly what it was. I didn't know they existed, nice call. I'd just copy and pasted the json from a browser window, somehow the mark was inserted in the process. Cheers!
I am unsure of the protocol regarding this item. The root cause of the problem has nothing to do with Python or with JSON and perhaps makes this item out of scope; it may be however that others will find themselves with the same issue. It is caused by my text editor - Emeditor - and the way it handles encodings. I have never noticed it before as the BOM is invisible. I had used vi as well during work, it didn't show the BOM characters either. There is an option within Emeditor to save without signature, this will fix the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.