-1

I want to compare the value of an object I have in a json file to the current time then print a response but I keep getting an error when trying to get the value. The function that checks the value of 'delta' compared to time.time() is connected to a button. The buttons are connected to the objects themselves. How would I fix this? Here is the code:

class MainApp(App):

    def build(self): # build() returns an instance
        self.store = JsonStore("streak.json") # file that stores the streaks

        return presentation

    def check_streak(self, instance):
        honey = self.store.get('delta')

        if honey > time.time():
            print("early") # test

        if honey == time.time():
            print("on time")

        if honey < time.time():
            print("late")

here is the object in the file:

{"first": {"action": "first", "action_num": "1", "seconds": 60, "score": 0, "delta": 1555714261.0438898}}

This is the error I get:

File "C:\Users\tonya\Desktop\realProjects\HaStreakual\HaStreakual.py", line 66, in check_streak
     honey = self.store.get('delta')
   File "C:\Python36\lib\site-packages\kivy\storage\__init__.py", line 159, in get
     return self.store_get(key)
   File "C:\Python36\lib\site-packages\kivy\storage\jsonstore.py", line 63, in store_get
     return self._data[key]
 KeyError: 'delta

1 Answers1

0

You're one level off. Try self.store['first']['delta'].

gmds
  • 19,325
  • 4
  • 32
  • 58
  • The thing is there will be more than one object added to the file so this will only work for the "first" object and I'll have to manually type every object key. – EastCoastYam Apr 19 '19 at 23:19
  • @EastCoastYam Then you should edit your question to more clearly specify what you want. Do you want to get the value corresponding to the `'delta'` key for every object in the JSON file and put them in a `list`? Will there be only one object with a `'delta'` key, and do you need help finding it? Is it something else? The data in your question, and the question itself, should be representative of your actual problem. – gmds Apr 19 '19 at 23:21
  • @EastCoastYam JSON is a hierarchical format so to access an item you must do it through your parent, if you want to obtain the value associated with a key you must go through the json through the hierarchy tree. – eyllanesc Apr 19 '19 at 23:25
  • I'm not sure I understand what you mean @eyllanesc – EastCoastYam Apr 19 '19 at 23:35
  • @EastCoastYam As indicated by the answer of gmds to access `delta` you must first do it through`first`, that is to say `delta` you do it through your parent `first`, in the answers of the question that I have marked how duplicate they indicate how to do that automatic search. In particular, check this answer: https://stackoverflow.com/a/39016088/6622587 – eyllanesc Apr 19 '19 at 23:37