Reading JSON With Raspberry Pi
by keebie81 in Circuits > Raspberry Pi
37267 Views, 16 Favorites, 0 Comments
Reading JSON With Raspberry Pi
![File Dec 26, 3 07 08 PM.jpeg](/proxy/?url=https://content.instructables.com/F4I/1ZPT/IK8KRO3N/F4I1ZPTIK8KRO3N.jpg&filename=File Dec 26, 3 07 08 PM.jpeg)
So for this tutorial I am going to try to explain how to get a JSON file and decode what data is in it and how to display the values out of it you need.
Configure Your Raspberry Pi
This tutorial will assume you have setup internet access and have a way to access the command line on the Raspberry Pi. Any model should work well for this.
Create an Account at Openweathermap.org
![openweathersite.jpg](/proxy/?url=https://content.instructables.com/F6O/1BUL/IK8KRO2C/F6O1BULIK8KRO2C.jpg&filename=openweathersite.jpg)
I am using openweathermap.org for this tutorial. You will need an account to get an API access key. You can use any other site for this tutorial also as long as it has a json file you can get access to from the Pi.
What the Data Will Look Like
![jsonexample.jpg](/proxy/?url=https://content.instructables.com/FGG/MU2T/IK8KRO29/FGGMU2TIK8KRO29.jpg&filename=jsonexample.jpg)
![hardfollow.jpg](/proxy/?url=https://content.instructables.com/FLY/O5GR/IK8KRO2W/FLYO5GRIK8KRO2W.jpg&filename=hardfollow.jpg)
![jsonlint.jpg](/proxy/?url=https://content.instructables.com/FEK/DD27/IK8KRO2A/FEKDD27IK8KRO2A.jpg&filename=jsonlint.jpg)
Openweather has an API section that covers what the data you receive back will look like. As you can see in the second picture its hard to decipher the data when its one long string. I use JSONLint to format it easier for me to see.
Write the Code
![code.jpg](/proxy/?url=https://content.instructables.com/FYB/WHU2/IK8KRO2K/FYBWHU2IK8KRO2K.jpg&filename=code.jpg)
Now lets create a python script on the Pi to run our query.
I entered "sudo nano weatherdata.py"
Now paste this in, if you want metric values uncomment that line and comment out the next line. you also need to put in your cityid
import json, requests
key = 'your api key'
#units = 'metric'
units = 'imperial'
cityid = 'your city id'
url = requests.get('http://api.openweathermap.org/data/2.5/weather?id='+cityid+'&units='+units+'&APPID='+key)
weather = json.loads(url.text)
mmhg = (weather['main']['pressure']*0.75006375541921)
print weather['main']['temp'],"F"
print int(mmhg),"mmHg"
print weather['weather'][0]['description']
How the Code Works
We make a variable named "url" which is the link to our json. You could use just one long link with the the api key, units and city id already in it. But that would have made it harder to understand for the tutorial.
The variable "weather" becomes the json data.
I want to convert the pressure value to mmHg so I created another variable "mmhg", to give it a value we have it point to our weather variable, then the main section, then pressure value. We multiply it by that number to convert to mmhg.
We want to print out the longitude value so we tell it to print our weather variable and we want coord and then the lon value in that section.
To print out the weather description is a bit trickier because the weather section in the JSON file came back with brackets also besides braces. So we tell it to print our weather variable, weather group, then we need an integer for which brace group, since there was only 1 it is 0 we put in. Then we ask for description value
Results
![good.jpg](/proxy/?url=https://content.instructables.com/F5S/N3Y2/IK8KRO2V/F5SN3Y2IK8KRO2V.jpg&filename=good.jpg)
![error.jpg](/proxy/?url=https://content.instructables.com/F0M/20U2/IK8KRO2S/F0M20U2IK8KRO2S.jpg&filename=error.jpg)
If everything is good we get back some data. But if we overlooked the file and a section we wanted had brackets also and we did not tell it which section to select we will get an error. The error is list indices must be integers, not str
Thanks
Hopefully this was helpful. I am new to coding so I might not have explained things well. If you have a suggestion or something I explained wrong let me know and I will try to fix it.
Youtube Video Covering This
Heres a Youtube video I made on this topic also