Reputation: 576
How to get every value of a key of a JSON file with multiple dicts? I want to extract every value of "username"
key.
data.json
{
"1476439722046238725": {
"tweet_id": "1476439722046238725",
"username": "elonmusk",
},
"1476437555717541893": {
"tweet_id": "1476437555717541893",
"username": "billgate",
},
"1476437555717541893": {
"tweet_id": "1476437555717541893",
"username": "jeffbezos",
This is what my code so far but it gave me this error KeyError: 'username'
.
main.py
import json
with open("data.json", "r") as f:
data = json.load(f)
print(data["username"])
Upvotes: 0
Views: 37
Reputation: 54698
You need to enumerate through the outer dictionary.
import json
with open("data.json", "r") as f:
data = json.load(f)
for val in data.values():
print( val['username'] )
Upvotes: 3