Getting to grips with JSON with Python

Bloggedin
5 min readDec 13, 2020

--

When it comes to data formats there are many but one of the most dominant ones of the modern computing era is JSON. JSON stands for JavaScript Object Notation and since the growth of API’s and JavaScript on the web its adoption is now widespread. Coming from a background of processing CSV’s and then XML, JSON by comparison at first is very daunting and alien in appearance.

At first processing JSON files seems very confusing but the more we use Node-RED and API’s the more we have come to realise what an excellent format it is. Even if you have not explored the aforementioned applications you will have no doubt seen a JSON file as they are now common as configuration files for programs.

JSON is a popular way to format data as a single string that is deemed human readable and if you program in Python there is a module we can use without the need to know JavaScript.

JSON grew out of the need for stateless real-time server-to-browser communication protocol without using browser plugins such as Flash or Java applets. It was originally specified by Douglas Crockford in the early 2000s.

If you aim to use web API’s then you will need to understand JSON in whatever language you code in, today we are going to look at Python but first let us examine the basic format of a JSON file.

How it looks

Typically JSON consists of elements and values collected inside an object. The collection of elements is set inside curly braces.

E.g.

{

“firstName”: “Pycro”,

“lastName”: “Coding”,

“age”: 45,

“address”: {

“street”, “27 Acacia avenue”,

“city”, “London”,

“postcode”, “LLC00LJ”

}

}

Note that elements can be nested just like our address items and that typically data is enclosed inside of quotes except for numerical values. Semi Colons split the data names and values and an element is terminated with a comma if further elements exist.

Python and Pythonista on the iPad

Now that we have explained the basics let us dive into Python use some API’s to gather JSON data and show how to access the data set with Python to extract key points. Since I am writing this on my iPad I will be using Pythonista, however the syntax is the same on whatever platform you choose to follow along on.

API’s to gather data are everywhere, some you need keys for, some are open and all you have to do is simply request the data I will add some links at the end of the article to point to sources of API’s you can play with, but first we will stick to our tried and trusted Open Notify links that we have used in a previous example using Node-RED. The API’s from Open Notify gives us data from the International Space Station (ISS) and well who doesn’t like space related data!

The first API we are going to use is the following URL

http://api.open-notify.org/iss-now.json

Which when browsed to gives us the location data of the ISS In JSON format.

{“iss_position”: {“longitude”: “-78.0714”, “latitude”: “-48.3344”}, “timestamp”: 1607250174, “message”: “success”}

Below shows a simple session in Pythonista gathering data from this API.

Using two standard modules json and requests we are able to interact with the API in Python, the code should be easy to understand and I have commented the stages. This example provides for a simple JSON next we will look results which return multiple sets.

For this we will use the ISS station pass times for our location and extract the duration of the passes for which there are multiple values.

The above code shows you how to loop through multiple entries using the length (len()) of the response elements.

So with minimal Python code we can request JSON data from an API and extract key information as required.

Building a JSON from within Python

For the observant amongst you JSON is not too dissimilar from a Python dictionary and we can quickly convert dictionary data to JSON within Python:

The complete API stack from Open Notify formatted

import json, requests, datetime#set longitude and latitudeLON = '3.4360'LAT = '55.3781'#urls for our 3 API callsurl1 = 'http://api.open-notify.org/iss-now.json'url2 ="http://api.open-notify.org/iss-pass.json?lat="+LAT+"&lon="+LON+""url3 ='http://api.open-notify.org/astros.json'locationresponse = requests.get(url1).json()passesresponse = requests.get(url2).json()crewresponse = requests.get(url3).json()#print(locationresponse)#print(passesresponse)#print(crewresponse)#extract parts of the data for printingprint("Currently the ISS is at : \nLongitude : "+str(locationresponse['iss_position']['longitude'])+"\nLatitude : "+str(locationresponse['iss_position']['latitude'])+" \n")print("Currently there is " + str(crewresponse['number'])+ " crew on board\n")print("Their names are : \n")for i in range(0,len(crewresponse['people'])):    print(crewresponse['people'][i]['name'])    print("\nAt your provided coordinates of "+LON+" Longitude and "+ LAT+ " Latitude you can expect "+ str(len(passesresponse['response']))+ " passes of the ISS at : \n")#loop through the times and durationsfor i in range(0,len(passesresponse['response'])):passtime =datetime.datetime.fromtimestamp(passesresponse['response'][i]['risetime']).strftime('%Y-%m-%d %H:%M:%S')duration = passesresponse['response'][i]['duration']print(passtime + " for " + str(duration) + " seconds")

You can see that API’s are very useful for collecting data and the JSON file format is handy and easy to process in Python. You can extract the data in any order you choose and equally present it how you see fit, you could use a flask website and JavaScript to format the data into a page table.

There is a wealth of API’s out there some free to explore others still free but require a key to use by registering some details, some are even part of your social media accounts that you can set up to extract data.

Some API’s to play about with

Cat facts https://cat-fact.herokuapp.com/facts

Bitcoin prices https://api.coindesk.com/v1/bpi/currentprice.json

Bored https://www.boredapi.com/api/activity/

Carbon intensity https://api.carbonintensity.org.uk

Open food facts https://world.openfoodfacts.org/api/v0/product/737628064502.json

(Provide bar codes for json)

Brewdog https://api.punkapi.com/v2/beers

Dungeons & Dragons https://www.dnd5eapi.co/api/spells/acid-arrow

Pokemon https://pokeapi.co

UK Postcodes http://api.postcodes.io/postcodes/<postcode>

COVID-19 https://covid-api.mmediagroup.fr/v1/cases?country=United%20Kingdom

Large list of mixed API’s with and without Auth

https://github.com/public-apis/public-apis#animals

You can also sign up at rapidapi for further API’s

Thanks for blogging in…

--

--

Bloggedin
Bloggedin

Written by Bloggedin

Author of https://www.bloggedin.co.uk, Coding Python, Raspberry Pi, micro:bit, IoT and Node-RED.

No responses yet