4

I have JSON data, I want to parse it.

I used Code bellow:

def json_update(request):
     j = urllib2.urlopen('http://ec2-72-44-51-113.compute-1.amazonaws.com:8001/get_latest_verification')
     j_obj = json.load(j) 
     print j_obj.status
     return HttpResponse(j_obj.status)

But I am getting the error:

AttributeError at /json_update

'list' object has no attribute 'status'

my json data:

[{"status": "Registered", "credential": "10000323xsds", "scan_time": "Jan.15,2014, 03:30 pm", "asset_code": "cls103", "emp_id": "LS07", "location": "BLR-10", "auth_code": "DSC(Verisign", "operator": "pradeep", "id": 538}]

what is the correct way of parsing json data.

But when I updated the code to:

UPDATED CODE:

def json_update(request):
     j = urllib2.urlopen('http://ec2-72-44-51-113.compute-1.amazonaws.com:8001/get_latest_verification')
     j_obj = json.load(j)
     for jo in j_obj: 
      print j_obj[jo]['status']
     return HttpResponse(j_obj[jo]['status'])

I am getting error:

TypeError at /json_update

list indices must be integers, not dict

1
  • Problem is not with json parsing. if you did j_obj[0]['status'] then it would work just fine. Commented Jan 15, 2014 at 10:34

2 Answers 2

8

You say your json data is:

[{"status": "Registered", "credential": "10000323xsds", "scan_time": "Jan.15,2014, 03:30 pm", "asset_code": "cls103", "emp_id": "LS07", "location": "BLR-10", "auth_code": "DSC(Verisign", "operator": "pradeep", "id": 538}]

This means when you do j_obj = json.load(j) your j_obj is a list. (Note the [ ] on the outside). That is why when you did print j_obj.status you got the error "'list' object has no attribute 'status'"

Your updated code does:

for jo in j_obj: 
    print j_obj[jo]['status']

This is the wrong way to use a for _ in _ loop in Python, you are doing it like you would in Javascript. In Python you would write it like this:

for jo in j_obj: 
    print jo['status']

This will loop through items in the outer list (you have a single item, which is a dict) and print the 'status' key of that dict.

You can see the for _ in _ loop gives you the actual item from the list you're looping over, not an index. So in your case jo is a dict, this is why you were getting the error "list indices must be integers, not dict" when you tried to do j_obj[jo].

See tutorial here:
https://wiki.python.org/moin/ForLoop

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

Comments

0

You should access the status as a dictionary item, not as an attribute. And the dictionary is an item of a list.

So the print, return statements should read as:

print j_obj[0]['status']
return HttpResponse(j_obj[0]['status'])

UPDATE

To print all key, value pairs:

for key, value in j_obj[0].items():
    print key, value
return HttpResponse('\n'.join( # OR   '<br>'.join
    '{} {}'.format(key, value)
    for key, value in j_obj[0].items()
))

1 Comment

yup all key value pairs...I updated the code u please check that also

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.