0

I have an issue with a code I've written. When I run the code I get an error :

Traceback (most recent call last):
File "test23_json_users.py", line 23, in <module>
for user_dict in abc['user_account']['sip_id']:
TypeError: list indices must be integers, not str

The code:

result = '[{"user_account":[{"address":null,"name":null,"country":null,"password":"****","extension":"1112","sip_id":"[email protected]","sip_name":"23001","user_id":7973712,"locked":false,"created":null,"h323_name":"1101","city":null,"email":null,"phone":null,"zip":null},{"address":null,"name":null,"country":null,"password":"****","extension":"1113","sip_id":"[email protected]","sip_name":"23002","user_id":8847075,"locked":false,"created":null,"h323_name":"1102","city":null,"email":null,"phone":null,"zip":null},{"address":null,"name":null,"country":null,"password":"****","extension":"1114","sip_id":"[email protected]","sip_name":"23003","user_id":3680630,"locked":false,"created":null,"h323_name":"1103","city":null,"email":null,"phone":null,"zip":null},{"address":null,"name":null,"country":null,"password":"****","extension":"1115","sip_id":"[email protected]","sip_name":"23004","user_id":136391,"locked":false,"created":null,"h323_name":"1104","city":null,"email":null,"phone":null,"zip":null},{"address":null,"name":null,"country":null,"password":"****","extension":"1116","sip_id":"[email protected]","sip_name":"23005","user_id":5692227,"locked":false,"created":null,"h323_name":"1105","city":null,"email":null,"phone":null,"zip":null},{"address":null,"name":null,"country":null,"password":"****","extension":"1117","sip_id":"[email protected]","sip_name":"23006","user_id":7559026,"locked":false,"created":null,"h323_name":"1106","city":null,"email":null,"phone":null,"zip":null},{"address":null,"name":null,"country":null,"password":"****","extension":"1118","sip_id":"[email protected]","sip_name":"23007","user_id":3226075,"locked":false,"created":null,"h323_name":"1107","city":null,"email":null,"phone":null,"zip":null},{"address":null,"name":null,"country":null,"password":"****","extension":"1119","sip_id":"[email protected]","sip_name":"23008","user_id":6184875,"locked":false,"created":null,"h323_name":"1108","city":null,"email":null,"phone":null,"zip":null},{"address":null,"name":null,"country":null,"password":"****","extension":"1120","sip_id":"[email protected]","sip_name":"23009","user_id":1711112,"locked":false,"created":null,"h323_name":"1109","city":null,"email":null,"phone":null,"zip":null},{"address":null,"name":null,"country":null,"password":"****","extension":"1121","sip_id":"[email protected]","sip_name":"23010","user_id":5383101,"locked":false,"created":null,"h323_name":"1110","city":null,"email":null,"phone":null,"zip":null}]}'

params = result[result.find('{"'):]
#print params
abc = json.loads(params)

# Here I'm trying to extract just sip_id from the string result using json.
# I am not able to figure out what went wrong .
for user_dict in abc['user_account']['sip_id']:
    print  'usersname : %s' % (user_dict['sip_id'])
2
  • when i use regx.. it works fine syntax used re.findall('(?<=\\"sip_id\\":\\")[a-zA-Z0-9]+@[a-zA-Z0-9.]+(?=\\")',result) Commented Jun 6, 2012 at 9:15
  • Pro tip: use from pprint import pprint, then pprint(abc) to get a much better feel of what your data structure looks like. Commented Jun 6, 2012 at 9:17

2 Answers 2

2

Change

for user_dict in abc['user_account']['sip_id']: 

to

for user_dict in abc['user_account']:
Sign up to request clarification or add additional context in comments.

Comments

1

abc['user_account'] is supposed to be a list.

A list a=['hello',1,2,'people'] has 4 elements accessed by indexes a[0] through a[3].

A dictionary d={'a':1,'b':2} has keys and values. In this case d['a'] has value 1, d[b'] has 2.

If you want to access something in abc['user_account'], type abc['user_account'][42]. If you want to iterate over it

for a in abc['user_account']:
  print a

4 Comments

Cool Works fine now . I tried this : for user_dict in abc['user_account']: print 'usersname : %s' % (user_dict['sip_id'])
Is there no way i can extract out just the sip id with one single line of code : for user_dict in abc['user_account']['sip_id']: print 'usersname : %s' % (user_dict['sip_id'])
If one answer solves your problem, please accept the correct answer by clicking the check mark. Do leave comments if you need more clarifications.
for a in abc['user_account']: ... print a['sip_id'] sorry for posting in single line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.