5

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.

I have this script so far:

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']

The JSON that it returns looks like this...

{
    "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",
},

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: print j[**0 through len(j)**]['name'] so it will print out the name (and preferably the phone number too) of every employee in the json list.

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

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
blindy
  • 55
  • 3
  • 1
    Have you tried using a for loop to go over your data and get your employee information? Hint, when you do `j[0]`, as you indicated, you are specifying your index of a `list`. Therefore, if you simply loop over that `j`, each iteration will be your dictionary and you can access your data through each iteration. – idjaw Sep 21 '16 at 00:21
  • Your example JSON is wrong. Wrap it within `[ ... ]` – Moinuddin Quadri Sep 21 '16 at 00:24

2 Answers2

6

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

for item in j:
    print item['name']

where j is result of j = json.loads(the_page) as is mentioned in your answer

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
3

Slightly nicer for mass-conversions than repeated dict lookup is using operator.itemgetter:

from future_builtins import map  # Only on Py2, to get lazy, generator based map
from operator import itemgetter

for name, phone_number in map(itemgetter('name', 'phone_number'), j):
    print name, phone_number

If you needed to look up individual things as needed (so you didn't always need name or phone_number), then regular dict lookups would make sense, this just optimizes the case where you're always retrieving the same set of items by pushing work to builtin functions (which, on the CPython reference interpreter, are implemented in C, so they run a bit faster than hand-rolled code). Using a generator based map isn't strictly necessary, but it avoids making (potentially large) temporary lists when you're just going to iterate the result anyway.

It's basically just a faster version of:

for emp in j:
    name, phone_number = emp['name'], emp['phone_number']
    print name, phone_number
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271