How to parse json data python django?

django, json, python

Solution

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

Problem

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

Original source