0

I have a JSON string that i got into python that looks something like this:

{"count": 100, 
 "facets": null, 
 "previous_page": null, 
 "results": [{"updated_at": "2013-09-17T13:45:13Z", "test_id": 194037042, "customer_id":       
              203793326, "id": 1954182}]

There are more elements but this is a small cut of what i need. Basically, results has a list of 100 dictionaries that contain the elements above "updated_at, test_id, customer_id, id" What i need to do is the following:

I need to get a list of all of the values of JUST the id. I am not sure how to go about this though, i have tried doing things like:

for i in my_dict['results']:
    print i['id']

but i get an error message that says:

print i['id']
TypeError: string indices must be integers

What am i doing wrong?

user2146933
  • 409
  • 5
  • 8
  • 16
  • possible duplicate of [Accessing JSON elements](http://stackoverflow.com/questions/16129652/accessing-json-elements) – DevlshOne Sep 17 '13 at 20:55
  • have you converted the string to python native data using `json.loads`? – Paulo Scardine Sep 17 '13 at 20:56
  • 1
    You are not doing anything wrong, but maybe not all of your data items have a `result` that is a list of dictionaries. Maybe one of your `results` is a list of string or a string. – Viktor Kerkez Sep 17 '13 at 20:56
  • Your code works *just fine* for the sample JSON you posted. The problem then, is that the error indicates the `my_dict['results']` value did not come from that chunk of JSON data. – Martijn Pieters Sep 17 '13 at 21:04

2 Answers2

0

You are not doing anything obviously wrong. Your data includes 'null' elements which are not proper python. This works fine.

my_dict = {"count": 100, 
 "facets": None, 
 "previous_page": None, 
 "results": [{"updated_at": "2013-09-17T13:45:13Z", "test_id": 194037042, "customer_id": 203793326, "id": 1954182}]
}

for i in my_dict['results']:
    print i['id']

The error you have implies that one of your list items is a string and when you are trying to get the ['id'] element the error rightly tells you that list indices (a string is a list of characters) must be integers.

Graeme Stuart
  • 5,837
  • 2
  • 26
  • 46
0

It looks like you haven't loaded the json into Python with json.loads(json_string)

And even after you do that, your "results" dict is actually going to be a list of dicts.

Try this:

import json

json_str = '{"count": 100, "facets": null, "previous_page": null, "results": [{"updated_at": "2013-09-17T13:45:13Z", "test_id": 194037042, "customer_id": 203793326, "id": 1954182}, {"updated_at": "2013-09-18T13:45:13Z", "test_id": 194037043, "customer_id": 203793327, "id": 1954183}]}'

data = json.loads(json_str)
result_ids = [result['id'] for result in data['results'] if 'id' in result]

Output:

[1954182, 1954183]

This code would then output a list containing 1954182 and 1954183. Use a list comprehension here for greater speed and less lines of code. It also ensures that the result dict has an 'id' attribute before trying to access it.