1

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', 
      'blah8','blah9','blah10']


   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()

 print(peep.getJSONdata("blah1"))
 print(peep1.getJSONdata("blah2"))  

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..

Davngr
  • 13
  • 5
  • Could you tell us what the expected output is? I'm having a hard time seeing what the end goal is. – TheF1rstPancake Nov 25 '16 at 18:44
  • @Jalepeno112 I added it to my post. I'd like like to call on any object in the list instead of just being able to call on the first object or all objects. maybe I'm looking at this the wrong way – Davngr Nov 25 '16 at 18:52
  • @ettanany is there a applicable difference between simplejson and json that I missed? – Davngr Nov 25 '16 at 18:54
  • They are the same: http://stackoverflow.com/questions/712791/what-are-the-differences-between-json-and-simplejson-python-modules. After a certain Python version, `simplejson` was added in as a standard as `json`. – TheF1rstPancake Nov 25 '16 at 18:59
  • 1
    You can use json or simplejson, I just suggested `json.loads()` if you have some issue with the other one. Your code is not well indented, so it is difficult to understand what you want to achieve. – ettanany Nov 25 '16 at 19:02
  • Looking at your `getJSONData` function, I'm not sure what you are trying to do. You have a for loop to iterate over each person, but then a return statement inside of it. This means that the loop will return the first value it finds in that loop. Did you mean to return the values for all people in the list? – TheF1rstPancake Nov 25 '16 at 19:07
  • @Jalepeno112 thanks. – Davngr Nov 25 '16 at 19:09
  • @ettanany Yes, I apologize for the structure. I just didn't want to post my actual project and got messy while paraphrasing it. .loads() causes error: raise TypeError("Input string must be text, not bytes") TypeError: Input string must be text, not bytes – Davngr Nov 25 '16 at 19:12
  • @Jalepeno112 yes, I think I'm going to have to uproot my code and start all over again. What I'm trying to do is access specific keys from the json file in every object. – Davngr Nov 25 '16 at 19:14
  • @Davngr for `json.loads()` you need to pass `peopleData.read()` (`json.loads(peopleData.read())`) but you do not need that sine `simplejson` is working for you. – ettanany Nov 25 '16 at 19:20

1 Answers1

0

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:

blah
blah

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'])
print(d)
# 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'}.

TheF1rstPancake
  • 2,318
  • 17
  • 17
  • Thank you very much for outlining a different path to take and I will take it into consideration. My issue is that it might not jive with the rest of the project requirements. – Davngr Nov 25 '16 at 21:16