Posts: 32
Threads: 11
Joined: Oct 2019
I have a list of dictionaries, each dictionary contains keys relating to its health.
I am creating the list via the following as I don't want ever key in the dictionary.
1 2 3 4 |
computer_keys = ( 'id' , 'hostname' , 'lastSeenAt' , 'threats' , 'ProtectionEnabled' , 'services' , 'ipv4Addresses' , 'associatedPerson' , 'health' )
for all_computers in computers_json[ "items" ]:
computer_dictionary = {key:value for key, value in all_computers.items() if key in computer_keys}
computer_list.append(computer_dictionary)
|
The list looks like this
[
{
"associatedPerson": {
"viaLogin": "user"
},
"encryption": {
"volumes": [
{
"status": "notEncrypted",
"volumeId": "60BB3F30"
}
]
},
"health": {
"overall": "good",
"services": {
"serviceDetails": [
{
"name": "service",
"status": "running"
},
The issue I am having is, Health will return the dictionary below it. I just need the overall key, not all others. How can I do this?
Posts: 4,220
Threads: 97
Joined: Sep 2016
Add a modification after line 3:
1 |
computer_dictionary[ 'health' ] = { 'overall' : computer_dictionary[ 'health' ][ 'overall' ]}
|
That assumes every computer dictionary has a health key. If not, you'll need a condition to check for that or you'll get a KeyError.
Posts: 32
Threads: 11
Joined: Oct 2019
Thanks Craig. That worked fine. Following the same logic I then added
1 |
computer_dictionary[ 'service_health' ] = { 'services' : computer_dictionary[ 'health' ][ 'services' ][ 'status' ]}
|
I thought this would add new key to the dictionary with the service health.
1 2 3 4 5 6 7 8 9 10 11 |
computer_keys = ( 'id' , 'hostname' , 'lastSeenAt' , 'threats' , 'service_health' , 'tamperProtectionEnabled' , 'ipv4Addresses' , 'associatedPerson' , 'health' )
for all_computers in computers_json[ "items" ]:
computer_dictionary = {key:value for key, value in all_computers.items() if key in computer_keys}
if 'health' in computer_dictionary.keys():
computer_dictionary[ 'health' ] = { 'overall' : computer_dictionary[ 'health' ][ 'overall' ]}
computer_dictionary[ 'service_health' ] = { 'services' : computer_dictionary[ 'health' ][ 'services' ][ 'status' ]}
computer_list.append(computer_dictionary)
|
If I comment out this line it works fine
1 |
computer_dictionary[ 'health' ] = { 'overall' : computer_dictionary[ 'health' ][ 'overall' ]}
|
I was thinking I would get the result of each sub key and then make a new key for the result. This will make it easier when I export it to CSV at the end.
Posts: 4,220
Threads: 97
Joined: Sep 2016
The code I suggested wipes out what was in computer_dictionary['health'] for the simpler dictionary with just 'overall'. If you want to pull something else from computer_dictionary['health'] as it was initially read, and put that into computer_dictionary['service_health'], you need to do that before my line of code.
Posts: 32
Threads: 11
Joined: Oct 2019
Thanks Craig. That is working perfectly now.
Here is the new code
1 2 3 4 5 6 7 8 |
for all_computers in computers_json[ "items" ]:
computer_dictionary = {key:value for key, value in all_computers.items() if key in computer_keys}
if 'health' in computer_dictionary.keys():
computer_dictionary[ 'service_health' ] = { 'services' : computer_dictionary[ 'health' ][ 'services' ][ 'status' ]}
computer_dictionary[ 'threats' ] = { 'threats' : computer_dictionary[ 'health' ][ 'threats' ][ 'status' ]}
computer_dictionary[ 'health' ] = { 'overall' : computer_dictionary[ 'health' ][ 'overall' ]}
computer_list.append(computer_dictionary)
|
The only issue left is, I really only want the bad or good, but I get the below. Is there anyway to tidy this up?
<class 'dict'>: {'id': '116bd3bf-00b9-42b1-bf11-ffc6de819084', 'hostname': 'DC', 'health': {'overall': 'good'}, 'ipv4Addresses': ['192.168.220.22'], 'ProtectionEnabled': True, 'lastSeenAt': '2018-11-14T17:07:28.240Z', 'service_health': {'services': 'good'}, 'threats': {'threats': 'good'}}
Posts: 4,220
Threads: 97
Joined: Sep 2016
If you just want the values, just assign the values:
1 2 3 4 5 6 7 8 |
for all_computers in computers_json[ "items" ]:
computer_dictionary = {key:value for key, value in all_computers.items() if key in computer_keys}
if 'health' in computer_dictionary.keys():
computer_dictionary[ 'service_health' ] = computer_dictionary[ 'health' ][ 'services' ][ 'status' ]
computer_dictionary[ 'threats' ] = computer_dictionary[ 'health' ][ 'threats' ][ 'status' ]
computer_dictionary[ 'health' ] = computer_dictionary[ 'health' ][ 'overall' ]
computer_list.append(computer_dictionary)
|
Posts: 32
Threads: 11
Joined: Oct 2019
Sorry Craig. I understand what you are saying, but I am clear on how to do that.
Posts: 4,220
Threads: 97
Joined: Sep 2016
Look at the code snippet I posted. You were assigning dictionaries: {key: value} pairs. I just took the value out of that expression and assigned it by itself.
Posts: 32
Threads: 11
Joined: Oct 2019
Thanks Craig, I will take a look. Sorry, I thought you were giving me a hint via my code :)
|