20

I'm getting my JSON from reddit.com, essentially something like this. I have done quite a bit of reading, but I don't really understand how I can grab the information I want from this JSON (I want a list of the story links). I understand that I can "decode" the JSON into a dictionary, but do I need to recur throughout the JSON to get what I need?

Thanks in advance.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
awegawef
  • 293
  • 1
  • 3
  • 6

3 Answers3

23

If you're using Python 2.6 or later, use the built-in json library. Otherwise, use simplejson which has exactly the same interface.

You can do this adaptively without having to check the Python version yourself, using code such as the following:

try:
    import json
except ImportError:
    import simplejson as json

Then, use json.loads() or whatever as appropriate.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
12
import urllib2
import json

u = urllib2.urlopen('http://www.reddit.com/.json')
print json.load(u)
u.close()
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

There are two ways you can "decode" json with Python, after you've parsed it into dicts and lists with the json library.

First, accessing it by indexes, like this:

url_list = [t['entries'][0]['url'] for t in data['windows'][0]['tabs']]

Or, you can iterate over its tree structure. The example function below isn't general purpose, it just illustrates that you need to consider JSON's three different kinds of "nodes" differently when parsing the tree. A key's "value" might be data, a list of child nodes with no keys, or a dict that's basically a new JSON object. You can't just run through checking every node for its name, data, and children like you would with a regular tree.

def depthFirstSearch(self, jsonobj, target, parentKey=None):
        if isinstance(jsonobj, dict):
            for key, value in jsonobj.items():
                if isinstance(value, (dict, list)):
                    self.depthFirstSearch(value, target, key)
                else:   # "data" node
                    if key == target and parentKey not in self.parentsToExclude:
                        self.results.append(value)
                        self.parents[parentKey] += 1
        if isinstance(jsonobj, list):
            for value in jsonobj:
                #lists don't have keys, pass along key from last dict
                self.depthFirstSearch(value, target, parentKey)
Noumenon
  • 5,099
  • 4
  • 53
  • 73