Question

    {
       "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 
Was it helpful?

Solution

Try this simpler version:

with open(r'C:/Json/Spain.json') as f:
    data = json.load(f)

for match in data['matches']:
    print(match['hteamName'])

It should get you started with the rest of your work.

OTHER TIPS

["hteamName :"+ x.get("hteamName") for x in d["matches"] ]
['hteamName :Spain', 'hteamName :Chile', 'hteamName :Germany', 'hteamName :Spain',    'hteamName :Netherlands', 'hteamName :Paraguay', 'hteamName :Spain']

In a function:

def search(d, k,nested):
    result=[]
    for n in d[nested]:
        result.append('{0} : {1}'.format(k,n.get(k)))
    return result

search(d,"hteamName","matches")
['hteamName : Spain', 'hteamName : Chile', 'hteamName : Germany', 'hteamName : Spain', 'hteamName : Netherlands', 'hteamName : Paraguay', 'hteamName : Spain']

This should get you started.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top