0

Super novice here, have looked around with no luck but it's really done my head in.

I'd like to pull corresponding values for all keys that match to a certain string.

In this case, I've got a GET request from the docker API listing containers. Two containers as shown, I'd like to print both their 'Id' and 'Names' values.

Also for the sake of curiosity, how would we go deeper into "PrivatePort" value?

Cheers

[
    {
        "Id": "aa303658b81be27e932bfe4dad4fd3c12f0fb149963391a28657ca8a51549b80",
        "Names": [
            "/silly_chatterjee"
        ],
        "Image": "lgcon-base-steamcmd:latest",
        "ImageID": "sha256:3439f862bf7114f2a08ef684b7ebaa3b79cd0577906f473f9e04b1acb4071a27",
        "Command": "bash /steamcmd/steamcmd.sh",
        "Created": 1606578178,
        "Ports": [
            {
                "PrivatePort": 8080,
                "Type": "tcp"
            },
            {
                "PrivatePort": 28015,
                "Type": "tcp"
            },
            {
                "PrivatePort": 28016,
                "Type": "tcp"
            },
            {
                "PrivatePort": 28082,
                "Type": "tcp"
            }
        ],
        "Labels": {},
        "State": "running",
        "Status": "Up 7 hours",
        "HostConfig": {
            "NetworkMode": "default"
        },
        "NetworkSettings": {
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "21d2b28c7ba993d9e056a2a43c8c5a048461fe820a3a861bd0b7720b101232ca",
                    "EndpointID": "01b60812abb596b22d51692caec97fb52ced498b5961c27aa9b3c67d6d71ea95",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        },
        "Mounts": []
    },
    {
        "Id": "93ffca099523727e2174e71bf0880e7b0edc3f7aa89279d37688f86061f237d5",
        "Names": [
            "/confident_wilbur"
        ],
        "Image": "lgcon-base-steamcmd:latest",
        "ImageID": "sha256:3439f862bf7114f2a08ef684b7ebaa3b79cd0577906f473f9e04b1acb4071a27",
        "Command": "bash /steamcmd/steamcmd.sh",
        "Created": 1606578168,
        "Ports": [
            {
                "PrivatePort": 28015,
                "Type": "tcp"
            },
            {
                "PrivatePort": 28016,
                "Type": "tcp"
            },
            {
                "PrivatePort": 28082,
                "Type": "tcp"
            },
            {
                "PrivatePort": 8080,
                "Type": "tcp"
            }
        ],
        "Labels": {},
        "State": "running",
        "Status": "Up 3 hours",
        "HostConfig": {
            "NetworkMode": "default"
        },
        "NetworkSettings": {
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "21d2b28c7ba993d9e056a2a43c8c5a048461fe820a3a861bd0b7720b101232ca",
                    "EndpointID": "802d3d63ce763999fc935b0ffcb77a394978e3608d4c7c3bb4f48664cde9bfb4",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:03",
                    "DriverOpts": null
                }
            }
        },
        "Mounts": []
    }
]

EDIT:

The following returns 'AttributeError: 'list' object has no attribute 'items'

import json

with open('test.json') as json_file:
    data = json.load(json_file)

def recursive_items(dictionary):
    for key, value in dictionary.items():
        if type(value) is dict:
            yield (key, value)
            yield from recursive_items(value)
        else:
            yield (key, value)



for key, value in recursive_items(data):
    print(key, value)
  • You will need to recursively iterate over the dictionary to get all keys/values, check out this question https://stackoverflow.com/questions/10756427/loop-through-all-nested-dictionary-values – Ron Serruya Nov 29 '20 at 10:47

1 Answers1

0

maybe this helps you:

# turn json string into nested dictionaries 
data_nested_dicts = json.loads(data_json)

#iterate over containers
for container in data_nested_dicts:
    # get Id and Names
    container_id = container['Id']
    container_names = container['Names']

    # "collect" private ports
    container_private_ports = []
    for port in container['Ports']:
        container_private_ports.append(port['PrivatePort'])

    # print the data 
    print(container_id, container_names, container_private_ports)

output:

aa303658b81be27e932bfe4dad4fd3c12f0fb149963391a28657ca8a51549b80 ['/silly_chatterjee'] [8080, 28015, 28016, 28082]
93ffca099523727e2174e71bf0880e7b0edc3f7aa89279d37688f86061f237d5 ['/confident_wilbur'] [28015, 28016, 28082, 8080]
Raphael
  • 1,731
  • 2
  • 7
  • 23
  • hey just curious, are for loops the most practical solution in this case? my python knowledge at the moment is quite minimal so no idea what other methods I should be learning for this. thanks again – Paurini Wiringi Nov 29 '20 at 11:26
  • In the very most cases for loops are the tool of choice to iterate over something. As a rule of thumb, I'd go as far as saying: use for loops unless you have a specific reason not to do so. If you want to go a bit further and simplify the creation of new lists or dicts you can have a look into pythons [list comprehension](https://www.pythonforbeginners.com/basics/list-comprehensions-in-python) – Raphael Nov 29 '20 at 14:33