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)