3

My database is formatted like this:

{
  "latitude" : 41.884629,
  "longitude" : -87.648764,
  "name" : "Bar Siena",
  "placeID" : "ChIJf3h_t9osDogReehZO7Hgk50",
  "vibes" : {
    "LGpswrvLgfYppcBG4bpmNzXQVoU2" : {
      "rating" : 1,
      "timestamp" : 1.477961061358844E9
    },
    "OSzA2KhTBWS3pxVnCZ6eScAuNDG3" : {
      "rating" : 5,
      "timestamp" : 1.477955566836665E9
    }
  }
}

I want to pull both the names of each bar, and every rating corresponding to it in the vibes table. My code looks like this:

firebase = pyrebase.initialize_app(config)

db = firebase.database()
for i in range(1,100):
    venues = db.child("venues").child(i).get()
    dict = (venues.val())
    print(dict['name'])

So far i'm only able to get the names, but i'm not sure how to go deeper and get the ratings. My database is managed using Firebase, and I'm using a python library called Pyrebase that's just a wrapper for the javascript api. I'm having trouble because each vibe has a generated hash key so i'm not sure how to go deeper. Any thoughts?

1
  • Can you show the format of the output you want to get? Commented Nov 15, 2016 at 15:43

3 Answers 3

1

vibes probably comes as a dictionary. What you want are the ratings of each hash key, so you can do something like

for hashkey, vibe_dict in dict['vibes'].items(): # dict is a Python keyword, so it's a bad choice for a variable!
    print(hashkey, vibe_dict['rating'])
Sign up to request clarification or add additional context in comments.

2 Comments

this is what I ended up using and it worked, didn't realize python would automatically map the key. Thank you!
@AmeerAkashe Glad I could help! If possible, please mark the answer as selected so that future viewers of your question know it's resolved :)
0

I am not sure about firebase, but in plain python you may do it like:

>>> for vibe in my_json['vibes'].values():
...     print vibe['rating']
...
1  # Rating of item 1
5  # Rating of item 2

where my_json is the dict build from your json:

{
   "latitude":41.884629,
   "longitude":-87.648764,
   "name":"Bar Siena",
   "placeID":"ChIJf3h_t9osDogReehZO7Hgk50",
   "vibes":{
      "LGpswrvLgfYppcBG4bpmNzXQVoU2":{
         "rating":1,
         "timestamp":1.477961061358844E9
      },
      "OSzA2KhTBWS3pxVnCZ6eScAuNDG3":{
         "rating":5,
         "timestamp":1.477955566836665E9
      }
   }
}

Comments

0

If your venues variable is a JSON-like object as in example, you can use the standart json library to work with it:

import json
dict_data = json.loads(venues)
print dict_data['latitude']

Btw, it's a bad style of development - to overwrite built-in Python funuctions. In your example you're overwriting dict(dict is a default Python type-object)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.