{
"matchesPlayed":{"label":"Matches Played","value":7}
, "matches":[
{
"date":"21 Jun"
,"hteamcode":"ESP"
,"hteamName":"Spain"
,"ateamcode":"HON"
,"ateamName":"Honduras"
,"score":"2:0 (1:0)"
}
,
{
"date":"25 Jun"
,"hteamcode":"CHI"
,"hteamName":"Chile"
,"ateamcode":"ESP"
,"ateamName":"Spain"
,"score":"1:2 (0:2)"
}
,
{
"date":"07 Jul"
,"hteamcode":"GER"
,"hteamName":"Germany"
,"ateamcode":"ESP"
,"ateamName":"Spain"
}
,
{
"date":"29 Jun"
,"hteamcode":"ESP"
,"hteamName":"Spain"
,"ateamcode":"POR"
,"ateamName":"Portugal"
,"score":"1:0 (0:0)"
}
,
{
"date":"11 Jul"
,"hteamcode":"NED"
,"hteamName":"Netherlands"
,"ateamcode":"ESP"
,"ateamName":"Spain"
,"score":"0:1 a.e.t."
}
,
{
"date":"03 Jul"
,"hteamcode":"PAR"
,"hteamName":"Paraguay"
,"ateamcode":"ESP"
,"ateamName":"Spain"
,"score":"0:1 (0:0)"
}
,
{
"date":"16 Jun"
,"hteamcode":"ESP"
,"hteamName":"Spain"
,"ateamcode":"SUI"
,"ateamName":"Switzerland"
,"score":"0:1 (0:0)"
}
]
}
This is a copy of my Json file. How can I loop through the json and make a list/or print a particular key (example: "hteamName"), which is similar in different arrays but with different values. From a previous question, I got some codes that could iterate through a nested dictionary, but it only finds unique keys with unique values.
def search(nest, nestitems):
found = []
for key, value in nest.iteritems():
if key == nestitems:
found.append(value)
elif isinstance(value, dict):
found.extend(search(value, nestitems))
elif isinstance(value, list):
for item in value:
if isinstance(item, dict):
found.extend(search(item, nestitems))
else:
if key == nestitems:
found.append(value)
return found