My Json Code:
{
"details" : [ {
"username":"testuser1",
"name":"User1",
"code":"50",
"schoolcode":"100",
"password":"abcd"
},
{
"username": "testuser2",
"name": "User2",
"code": "60",
"schoolcode": "100",
"password": "1234"
}
]
}
Python Code:
import json
f = open("data.json" ,)
data = json.load(f)
print(data["details"])
Output:
[{'username': 'testuser1', 'name': 'User1', 'code': '50', 'schoolcode': '100', 'password': 'abcd'}, {'username': 'testuser2', 'name': 'User2', 'code': '60', 'schoolcode': '100', 'password': '1234'}]
Here I want to store the usernames in a list using python.
CodePudding user response:
Loop over the list and pick the required attribute
data = {
"details" : [ {
"username":"testuser1",
"name":"User1",
"code":"50",
"schoolcode":"100",
"password":"abcd"
},
{
"username": "testuser2",
"name": "User2",
"code": "60",
"schoolcode": "100",
"password": "1234"
}
]
}
users = [x['username'] for x in data['details']]
print(users)
output
['testuser1', 'testuser2']