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.

2
  • When you googled Python and JSON, what did you find? Did you try "Python Library JSON"? What did you see? Commented Feb 25, 2010 at 11:15
  • 37
    When I googled python decode json I found this post.... Commented Nov 11, 2011 at 2:55

3 Answers 3

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.

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

1 Comment

Note that an open url gives a file-like object, so json.load is probably the appropriate function.
12
import urllib2
import json

u = urllib2.urlopen('http://www.reddit.com/.json')
print json.load(u)
u.close()

Comments

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.