5

I have the following json:

{"sensors": 
        {"-KqYN_VeXCh8CZQFRusI": 
            {"bathroom_temp": 16,
             "date": "02/08/2017", 
             "fridge_level": 8,
             "kitchen_temp": 18, 
             "living_temp": 17, 
             "power_bathroom": 0, 
             "power_bathroom_value": 0, 
             "power_kit_0": 0
        }, 
        "-KqYPPffaTpft7B72Ow9": 
            {"bathroom_temp": 20, 
             "date": "02/08/2017", 
             "fridge_level": 19, 
             "kitchen_temp": 14, 
             "living_temp": 20, 
             "power_bathroom": 0, 
             "power_bathroom_value": 0
        },  
        "-KqYPUld3AOve8hnpnOy": 
            {"bathroom_temp": 23, 
             "date": "02/08/2017", 
             "fridge_level": 40, 
             "kitchen_temp": 11, 
             "living_temp": 10, 
             "power_bathroom": 1, 
             "power_bathroom_value": 81, 
        }
    }
}

This is the python code that I have so far:

from flask import Flask, render_template, json, url_for
from firebase import firebase
import os




firebase = firebase.FirebaseApplication('https://my-firebase-db-958b1.firebaseio.com/', None)
result = firebase.get('/Dublin-Ireland', None)
print "\n Json file created!\n"

data_temp = result.values()[0]

for key, value in data_temp.iteritems():
    print key
    print value
    print "\n\n"

With the current code, I'm able to view the keys "KqYN_VeXCh8CZQFRusI", "KqYPPffaTpft7B72Ow9", etc.. and the values for those keys which are:

{"bathroom_temp": 16,
             "date": "02/08/2017", 
             "fridge_level": 8,
             "kitchen_temp": 18, 
             "living_temp": 17, 
             "power_bathroom": 0, 
             "power_bathroom_value": 0, 
             "power_kit_0": 0
        }, 

and

 {"bathroom_temp": 20, 
         "date": "02/08/2017", 
         "fridge_level": 19, 
         "kitchen_temp": 14, 
         "living_temp": 20, 
         "power_bathroom": 0, 
         "power_bathroom_value": 0
    },  

I need to get the secondlevel keys and their values. I'm trying to have a file for each key ("bathroom_temp", "power_bathroom", etc...) and save all the corresponding values of those keys in the file.

For example, the file "bathroom_temp.txt" will have the values "16, 20, 23".

1

1 Answer 1

4

You can put your JSON data in a variable:

data = {"sensors":
        {"-KqYN_VeXCh8CZQFRusI":
            {"bathroom_temp": 16,
             "date": "02/08/2017",
             "fridge_level": 8,
             "kitchen_temp": 18,
             "living_temp": 17,
             "power_bathroom": 0,
             "power_bathroom_value": 0,
             "power_kit_0": 0
        },
        "-KqYPPffaTpft7B72Ow9":
            {"bathroom_temp": 20,
             "date": "02/08/2017",
             "fridge_level": 19,
             "kitchen_temp": 14,
             "living_temp": 20,
             "power_bathroom": 0,
             "power_bathroom_value": 0
        },
        "-KqYPUld3AOve8hnpnOy":
            {"bathroom_temp": 23,
             "date": "02/08/2017",
             "fridge_level": 40,
             "kitchen_temp": 11,
             "living_temp": 10,
             "power_bathroom": 1,
             "power_bathroom_value": 81,
        }
    }
}

and then use a nested index address for getting the desired parameter:

kitchen_temp = data["sensors"]["-KqYN_VeXCh8CZQFRusI"]["kitchen_temp"]
print(kitchen_temp)
Sign up to request clarification or add additional context in comments.

1 Comment

That works for the first set of data, i have hundreds of these, and the 1st level values (eg. KqYPPffaTpft7B72Ow9) is random and i do not have control over the name of this.

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.