Forums » Developers

A Python example of how to get a JSON value from the API

I'm learning Python and used the Clicky API as a small project to get todays visitors. It's very basic but it does the job. Just wanted to share it and maybe it helps someone to get started ;-)

I use REQUESTS for the http handeling and JSON decoding. You can install it by using: pip install requests

Have fun!


# code starts below

import requests

# Set the request parameters
url = ''

# Fetch url
print("Fetching url..")

# Do the HTTP get request
response = requests.get(url, verify=True) #Verify is check SSL certificate

# Error handling

# Check for HTTP codes other than 200
if response.status_code != 200:
print('Status:', response.status_code, 'Problem with the request. Exiting.')

# Decode the JSON response into a dictionary and use the data

data = response.json()

visitors = data[0]['dates'][0]['items'][0]['value']

print('nVisitors today:'), visitors

# code ends

Posted Thu Jan 22 2015 4:05am by rhoekman

Reply to this discussion

You must be logged in to your account to post!