Reputation: 179
Below is my json data. What I want to do is write a python function to loop out the values of each id. Would anybody please help me with this?
[
{
"id": "1",
"code": "111",
},
{
"id": "2",
"code": "222",
},
{
"id": "3",
"code": "333",
}
]
Upvotes: 1
Views: 2313
Reputation: 250981
>>> data=[{"id": "1","code": "111",},{"id": "2","code": "222",},{"id": "3","code": "333",}]
>>> list_id=[x['id'] for x in data]
>>> print(list_id)
['1','2','3']
Upvotes: 1
Reputation: 86
If jsondata is a Python list with your example data, you can do:
ids = [item['id'] for item in jsondata]
Upvotes: 2
Reputation: 122376
If jsondata
is a Python list with your example data, you can do:
for item in jsondata:
print item['id']
Upvotes: 3