Davngr Davngr - 1 month ago 10
JSON Question

how do i iterate through json objects using python

The json file I'm reading from looks like this, I decided to not to post the actual file since it's for a class project and don't feel comfortable posting that(it is validated):

{ "peoples": [
{blah1 : blah, blah2: blah,blah3 : blah, blah4: blah,blah5 : blah,
blah6:blah,blah7 : blah, blah8: blah,blah9 : blah, blah10: blah,}

{blah1 : blah, blah2: blah,blah3 : blah, blah4: blah,blah5 : blah,
blah6:blah,blah7 : blah, blah8: blah,blah9 : blah, blah10: blah,}

My Code on python looks like this so far, I'm not entirely sure that this is the best way to code for this situation but it does work partially and why I'm asking for assistance:

import simplejson

class People(dict):
def __init__(self):
self.tricks = []
_keys = ['blah1', 'blah2', 'blah3','blah4', 'blah5', 'blah6','blah7',

def getJSONdata(self,tricks):
with open(Dfile.json, 'r') as peopleData:
dataObject = simplejson.load(peopleData)
for people in dataObject['peoples']:
return (people[tricks])

peep = People()
peep1 = People()


I can access anything on the object using keys but can't access the second, etc... I have looked through multiple json and python sites and nothing really addresses this issue. Any help would be appreciated.

I'd like to be able to access the objects equally. so i could get blah1 from object1, object2 etc..


I think your setup is a little overkill for what you want to do.

Let's say your JSON file looks like this:

{"peoples": [
     {"blah8": "blah", "blah2": "blah", "blah4": "blah", "blah3": "blah", "blah9": "blah", "blah7": "blah", "blah1": "blah", "blah6": "blah", "blah5": "blah", "blah10": "blah"}, 
     {"blah8": "blah", "blah2": "blah", "blah4": "blah", "blah3": "blah", "blah9": "blah", "blah7": "blah", "blah1": "blah", "blah6": "blah", "blah5": "blah", "blah10": "blah"}

Each object in peoples is supposed to represent a person. Using json you can load the data from your JSON file into a Python dictionary:

import json

dataObject = json.load(open("myjsonfile.json", "r"))

Then, dataObject['peoples'] is the array of people. dataObject['peoples'][0] is the first person. dataObject['peoples'][1] is the second person and so on.

So now if you want to iterate through the each person and get the value for a given key:

key = "blah2"
for p in dataObject['peoples']:
    print(p.get("blah2", None))

Which will print the "blah2" key for each person. If the key is not found, then it will return None. This is simply a safety in case you end up with a person who doesn't have a particular key. So the output here should be:


Overall, creating an object is not necessary. You can achieve this functionality by simply loading the json data and then using a function:

import json

dataObject = json.load(open("myjsonfile.json", "r"))

def getDataFromPersons(key, persons):
   # build a list of the values from each person with the given key
   return [p.get(key, None) for p in persons]

d = getDataFromPersons("blah2", data['persons'])
# blah
# blah

However, you likely want to be able to keep track of which person the data came from. In that case you'd want to build a dictionary instead of a list:

def getDataFromPersons(key, persons):
   # build a dictionary where the key is the index of the person in the list
   # and the value is the value for that person at the given key
   return {i:p.get(key, None) for i,p in enumerate(persons)}

This would output {0: 'blah', 1: 'blah'}.