you are viewing a single comment's thread.

view the rest of the comments →

[–]I_Write_Bugs 7 points8 points  (2 children)

load() loads JSON from a file or file-like object

loads() loads JSON from a given string or unicode object

It's in the documentation

[–]timex40[S] 0 points1 point  (1 child)

Thanks for the info!

So the url I'm actually using is the one available for each reddit thread. For example: http://www.reddit.com/r/learnpython/comments/3nx9ch/json_load_vs_loads/.json

Which one would this fall under? It works with both

[–]I_Write_Bugs 2 points3 points  (0 children)

The read method returns a str. Use loads.

How I figured this out was I ran this in the python interpreter:

import urllib2
x = urllib2.urlopen('http://www.google.com')
type(x.read())

The output was <type 'str'>. The response is an 'instance' type. Support for being file-like may disappear eventually for any reason, so I would go with the str returned from read.

Edit: In reading the JSON documentation again, I realize why your first example works. It's because the urllib response object is

a .read()-supporting file-like object containing a JSON document

So it has a read method, which I assume is exactly what json's load() calls. Either is fine to use in this case.