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.
Step 1: 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.
Step 2: Create an Account at Openweathermap.org
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.
Step 3: What the Data Will Look Like
Step 4: Write the Code
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)
Step 5: 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
Step 6: Results
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
Step 7: 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.
Step 8: Youtube Video Covering This
Heres a Youtube video I made on this topic also