Objective

Parse JSON data in Python.

Distributions

This will work on any Linux distribution.

Requirements

A working Linux install with Python.

Difficulty

Easy

Conventions

  • # - requires given command to be executed with root privileges either directly as a root user or by use of sudo command
  • $ - given command to be executed as a regular non-privileged user

Introduction

JSON is a favorite among developers for serializing data. It's used in most public APIs on the web, and it's a great way to pass data between programs. Python has no problem reading JSON.

Set Up

Before you can start working with JSON in Python, you'll need some JSON to work with. There are a few things that you'll need to set up first. First, create a Python file that will hold your code for these exercises. Inside the file, import the JSON module.
import json
Then, create some basic JSON. It shares almost identical syntax with a dictionary, so make a dictionary, and Python can use it as JSON.
json_data = '{"a": 1, "b"": 2, "c": 3, "d": 4, "e": 5}'
The next thing that you'll need is a JSON file. If you're not familiar, they are text files with the .json extension. Use your text editor of choice to make one and name it distros.json. Put the JSON below in the file
[
	{
		"Name": "Debian",
		"Version": "9",
		"Install": "apt",
		"Owner": "SPI",
		"Kernel": "4.9"
	},
	{
		"Name": "Ubuntu",
		"Version": "17.10",
		"Install": "apt",
		"Owner": "Canonical",
		"Kernel": "4.13"
	},
	{
		"Name": "Fedora",
		"Version": "26",
		"Install": "dnf",
		"Owner": "Red Hat",
		"Kernel": "4.13"
	},
	{
		"Name": "CentOS",
		"Version": "7",
		"Install": "yum",
		"Owner": "Red Hat",
		"Kernel": "3.10"
	},
	{
		"Name": "OpenSUSE",
		"Version": "42.3",
		"Install": "zypper",
		"Owner": "Novell",
		"Kernel": "4.4"
	},
	{
		"Name": "Arch Linux",
		"Version": "Rolling Release",
		"Install": "pacman",
		"Owner": "SPI",
		"Kernel": "4.13"
	},
	{
		"Name": "Gentoo",
		"Version": "Rolling Release",
		"Install": "emerge",
		"Owner": "Gentoo Foundation",
		"Kernel": "4.12"
	}
]

Parse Simple JSON

Everything's ready. Take a step back to that simple line of JSON that you created earlier. That's what you'll be working with first.

Python uses the loads method from the json to load JSON from a string. Did you notice the quotes around that dictionary that you created for the JSON? That's because Python treats JSON as a string unless it's coming from a file. You don't really need to worry about it too much. Just know that this form handles data while load handles files. Try loading the JSON.
print(json.loads(json_data)
It won't look much different, but Python sees it in a usable form now. You can save it to a variable and iterate over it to see.
loaded_json = json.loads(json_data)
for x in loaded_json:
	print("%s: %d" % (x, loaded_json[x]))
As you can see, loaded_json contains a dictionary, not a string that looks like one.

Parse To An Object

JSON is actually an object in JavaScript, so it would make sense to want to import it as an object in Python. There are a few ways to do this, but most involve creating a class that you instantiate by populating it with data from the JSON. There really isn't a direct conversion.

There is a fairly direct way to do it by loading the JSON into an object's __dict__ property.
class Test(object):
    def __init__(self, data):
	    self.__dict__ = json.loads(data)

test1 = Test(json_data)
print(test1.a)

Parse a JSON File

You're really not going to need to parse JSON from within a Python program. That doesn't make much sense in practicality. You will need to read and parse it from files, though, and that's why you set up that distros.json file.

A with can simplify the process of reading and closing the file, so that's the structure to use here. The other thing to note is the load method replaces loads because this is a file. Otherwise, the process is mostly the same.
with open('distros.json', 'r') as f:
    distros_dict = json.load(f)

for distro in distros_dict:
    print(distro['Name'])

Closing Thoughts

It's really not hard to parse JSON in Python. By using the json.load methods, you can convert the JSON into a dictionary. That dictionary can be used as a dictionary, or it can be imported into an object as it's instantiated to transfer data into a new object.

Exercises

  1. Create a new Python file an import JSON
  2. Crate a dictionary in the form of a string to use as JSON
  3. Use the JSON module to convert your string into a dictionary.
  4. Write a class to load the data from your string.
  5. Instantiate an object from your class and print some data from it.
  6. Create a JSON file with some JSON in it.
  7. Import your JSON file into Python and iterate over the resulting data.