python dictionaries and JSON (crash course)

This week we will have a quick look at the use of python dictionaries and the JSON data format. After reading this post, you should have a basic understanding how to work with JSON data and dictionaries in python. I’ll choose this topic because of some future posts about the work with python and APIs, where a basic understanding of the data format JSON is helpful.

I add the (unspectacular) examples from this post to my python script examples repository on GitHub.

What is a python dictionary?

A python dictionary is a list that is indexed by key values. The key value can be any immutable data type, strings are used in most cases. The dictionary data structure is also often called “associative arrays” in other programming languages.

A dictionary can be created using the function dict(). To access a value in a python dictionary, you use the square brackets after the variable name, as the following example describes:

my_dictionary["my key"] = "Value"
print(my_dictionary["my key"])</pre>

If the key is not found in the dictionary, an exception is thrown. To get a list of all defined keys in a dictionary, you can use the method .keys() on the dictionary object. You can also create python dictionaries using curly braces, as described in the following example:

my_dict = {
    "key 1": "value 1",
    "key 2": "value 2"

You can also add lists and nested dictionaries to the data structure and access it using multiple square brackets. The use of python dictionaries is straightforward and makes from my perspective the code more readable in many cases. If you like to know more about the python dictionary data structure, you can find further information in the official python documentation.

What is JSON?

JSON stands for JavaScript Object notation and is an open standard human readable data format. It is defined in RFC 7159. Many APIs work with this format to provide and receive data, as we had seen in the post about the Cisco NX-API on Nexus 5500. Popular alternatives to JSON are YAML and XML. The Wikipedia article about JSON provides a very good overview and explanation, therefore I will just summarize the basic formatting elements.

An empty JSON file simply contains two curly braces {}. The following example describes some basic value definitions. Multiple value statements in JSON are separated by a comma sign.

    "text key": "string",
    "integer key": 123,
    "boolean key": true,
    "simple list": [
        "value 1",
        "value 2"
    "dict": {
        "element 1": "value 1",
        "element 2": "value 2"
    "dict list": [
            "key 1.1": "value 1.1",
            "key 1.2": "value 1.2"
            "key 2.1": "value 2.1",
            "key 2.2": "value 2.2"

You might have noticed that these definitions are quite similar to the value definitions within a python dictionary.

Work with dictionaries and JSON data in python

To work with JSON formatted data in python, we will use the integrated python json module. We can use this module to load any JSON formatted data from a string or a file, as the following code example describes:

import json

# you can also use the open function to read the content of a JSON file to a string
json_data = """ {
    "key 1": "value 1",
    "key 2": "value 2",
    "decimal": 10,
    "boolean": true,
    "list": [1, 2, 3],
    "dictionary": {
        "child key 1": "child value",
        "child key 1": "child value"

my_dict = json.loads(json_data)

You can now work with the data as with regular python dictionaries.

print("string value: %s" % my_dict["key 1"])
print("decimal value: %d" % my_dict["decimal"])
print("decimal value: %r" % my_dict["boolean"])
print("list values: %s" % my_dict["list"])

You can use the dumps() function to create a JSON formatted string from a dictionary, like in the following example:

import json

my_dictionary = {
    "key 1": "Value 1",
    "key 2": "Value 2",
    "decimal": 100,
    "boolean": False,
    "list": [1, 2, 3],
    "dict": {
        "child key 1": "value 1",
        "child key 2": "value 2"


The output from this dumps command is quite complicated, because it won’t use any kind of indentation by default. To create a more readable output, you can add the indent parameter to the function call, which will add some hierarchy and indention to the output.

print(json.dumps(my_dictionary, indent=4))

By default, the keys within a python dictionary are unsorted and the output of the json.dumps() function may be different when executing multiple times. I ran into this issue while writing some test cases, but setting the sort_keys parameter to true will solve the problem. This will sort the key values of the dictionary and will produce always the same output when using the same data.

print(json.dumps(my_dictionary, indent=4, sort_keys=True))

Summary and wrap up

I work a lot with the JSON format in my scripts, because of the simplicity and the better readability. Furthermore it can be used by many other modules and use cases, as you can see in the configuration generator with python and Jinja2 post. We use in this case a python dictionary to provide the parameters for the templates.

Within the next weeks, I will dive deeper in the use of REST APIs with python. For these examples, a basic understanding of the JSON format and the python dictionary is necessary. If you have any questions, and/or additional information, please leave a comment below. Thanks for reading.

Henry Ölsner avatar
About Henry Ölsner
Just another Network Engineer and hobby programmer