Reputation: 19
I have a list of dictionaries which I sent from my javascript file to my view. Now, when i print the list, its type is shown as unicode. I want to get back the list I originally had. How can I do that?
My list :
[{"employee_id":13,"admin":false,"approver":false,"emp_code_or_email":"[email protected],120","manager":false,"emp_name":"second emp","department":"IT","position":"IT","new_emp":false,"manager_and_approver":false,"role_type":"employee","s_no":1},{"employee_id":144,"admin":false,"approver":false,"emp_code_or_email":"[email protected],1","manager":true,"emp_name":"A1 A2","department":"IT","position":"Developer","new_emp":false,"manager_and_approver":false,"role_type":"manager","s_no":2}]
I just want to fetch the employee_ids from this.
I tried eval() , list(), encode('utf-8'). But to no avail.
Upvotes: 1
Views: 84
Reputation: 35109
What you have is a string representation of a JSON. I can tell it by looking at "admin":false
in order for you to get employee_id
's. You first need to convert every json representation you have in the list into dictionary.
>>> import json
>>> data_raw = '''[{"employee_id":13,"admin":false,"approver":false,"emp_code_or_email":"[email protected],120","manager":false,"emp_name":"second emp","department":"IT","position":"IT","new_emp":false,"manager_and_approver":false,"role_type":"employee","s_no":1},{"employee_id":144,"admin":false,"approver":false,"emp_code_or_email":"[email protected],1","manager":true,"emp_name":"A1 A2","department":"IT","position":"Developer","new_emp":false,"manager_and_approver":false,"role_type":"manager","s_no":2}]'''
>>> data_json = json.loads(data_raw)
>>> [data['employee_id'] for data in data_json]
[13, 144]
>>>
Upvotes: 1
Reputation: 1563
When you print any objects, python will display python representation of this object.
To display it the way you want, you should format your output.
But easiest way will be to convert it to json, since you recieve it from ajax.
import simplejson
simplejson.dumps(data)
Upvotes: 0