0
    {
       "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 
Hammad Haleem
  • 1,374
  • 1
  • 16
  • 26
TheSoldier
  • 484
  • 1
  • 5
  • 25
  • What programming language is this question about? – pintxo May 08 '14 at 11:50
  • Can you please provide a sample output that you would like to get ? for this input and keyword for the search function. – Hammad Haleem May 08 '14 at 12:05
  • "hteamName":"Spain", "hteamName":"Chile", "hteamName":"Germany", "hteamName":"Spain", "hteamName":"Netherlands", "hteamName":"Paraguay", "hteamName":"Spain" – TheSoldier May 08 '14 at 12:06
  • Also, I'm loading the json array from a json file, and the json file is loaded into a python object - json_data=open('C:/Json/Spain.json') data = json.load(json_data) – TheSoldier May 08 '14 at 12:10

2 Answers2

1
["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.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • Thanks, but actually I'm trying to extract the key, value pairs to draw nodes for a graph, not just to print them. So either I'll have to make a list, or iterate through the whole json file (with a loop) and automatically pick the key, value pairs – TheSoldier May 08 '14 at 12:26
  • I was getting syntax errors at Search@(d,'hteamName', 'matches'), but the first one worked fine – TheSoldier May 08 '14 at 13:50
  • @TheSolider. sorry, missed a closing ], should work now – Padraic Cunningham May 08 '14 at 13:55
1

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.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • Thanks, but actually I'm trying to extract the key, value pairs to draw nodes for a graph, not just to print them. So either I'll have to make a list, or iterate through the whole json file (with a loop) and automatically pick the key, value pairs – TheSoldier May 08 '14 at 12:32
  • Thanks, this got me started. – TheSoldier May 08 '14 at 12:43