How do I pull a recurring key from a JSON?
<p>I'm new to python (and coding in general), I've gotten this far but I'm having trouble. I'm querying against a web service that returns a json file with information on every employee. I would like to pull just a couple of attributes for each employee, but I'm having some trouble.</p>

<p>I have this script so far:</p>

<pre><code>import json
import urllib2

req = urllib2.Request('http://server.company.com/api')
response = urllib2.urlopen(req)
the_page = response.read()

j = json.loads(the_page)

print j[1]['name']
</code></pre>

<p>The JSON that it returns looks like this...</p>

<pre><code>{
    "name": bill jones,
    "address": "123 something st",
    "city": "somewhere",
    "state": "somestate",
    "zip": "12345",
    "phone_number": "800-555-1234",
},
{
    "name": jane doe,
    "address": "456 another ave",
    "city": "metropolis",
    "state": "ny",
    "zip": "10001",
    "phone_number": "555-555-5554",
},
</code></pre>

<p>You can see that with the script I can return the name of employee in index 1. But I would like to have something more along the lines of: <code>print j[**0 through len(j)**]['name']</code> so it will print out the name (and preferably the phone number too) of every employee in the json list.</p>

<p>I'm fairly sure I'm approaching something wrong, but I need some feedback and direction.</p>

<p>Your JSON is the <code>list</code> of <code>dict</code> objects. By doing <code>j[1]</code>, you are accessing the item in the list at index <code>1</code>. In order to get all the records, you need to iterate all the elements of the list as:</p>

<pre><code>for item in j:
    print item['name']
</code></pre>

<p>where <code>j</code> is result of <code>j = json.loads(the_page)</code> as is mentioned in your answer</p>

python json