Python Code

Nested Dictionaries

Summary : Read a JSON file, create a Dict, loop through and get keys and values from the inner Dict using Python.

uses : / json.dumps / json.loads / list comprehension / for loop
Start with a JSON file – which looks like this in Notepad….

This post assumes you already have a nested dictionary saved as a JSON file. If not, you can download my json example.

We want to do 3 things:

  1. Import the JSON into a Dict type object

2. Display the Dict to check the format

3. Loop through the Dict to get the values that we need to print out.

Let’s get started,

Import the JSON file

First we need to use “import json” to work with the json format

Next, open the file “ytdict.json” and load it into an object

with open("ytdict.json") as f:
d = json.loads(

print(type(d)) # will give : <class 'dict'>

Check the contents, sort by int, otherwise 11 will appear before 2

Print the contents

print (json.dumps({int(x):d[x] for x in d.keys()}, indent=2, sort_keys=True))

The output should look like this :

Here you can see the outer dict, and then the inner dict with 2 key:value pairs

For loop with Nested Dictionaries

Write a new file, which will be the one we want to use as our actual program; ‘’

import json
# The 'real' code used in
with open("ytdict.json") as f:
    data = json.loads(
    # nested dictionary needs 2 for loops
    for k,v in data.items():
        for v2 in v:
        	print (v2[1])

We loop through the outer dictionary, finding the key:value pairs, and then using the second for loop we iterate through the key:value pairs from the returned values of the outer dictionary.

We’ve picked out the “Title” and “Description” values from the JSON file (It’s from my ‘YouTube-Channel-Downloader’ : Script)

Note the index of [1] – That picks out the values from the values in the first for loop.

If we had used [0] we would have ended up with this:

Iterating through Nested Dictionaries | JSON – watch the YouTube Video

Python Code

Automating a potentially repetitive task

Write 102 workplans or write some code to do it?

The was a requirement to write 102 workplans and word/docx files. The only data that was unique to each workplan was held in a csv – 1 row per workplan, so this is how we automated the creation of 102 word documents.

Python Code

Using an API

How to use a YouTube API to get images and video descriptions from any channel using Python.

This shows how you can use some code to get the thumbnails and descriptions for every video on a chosen YouTube channel. The output from the descriptions can be saved as a local copy for editing and re-uploading.

We have provided a guide on how to get the API key and clone our code.

Feel free clone the redandgreen YouTube Description Updater from GitHub :

Once you have set this up you can back up your metatdata and add additional text to the descriptions on ALL of your videos.

The downloader allows you to download Titles, Descriptions and Video ID to a JSON file. You can do this for ANY channel.

Also you can choose to download all of the high resolution thumbnail images.