Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Retrieving dictionary keys within with another dictionay
#1
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.

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?
Quote
#2
Add a modification after line 3:

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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
Thanks Craig. That worked fine. Following the same logic I then added

computer_dictionary['service_health'] = {'services': computer_dictionary['health']['services']['status']}
I thought this would add new key to the dictionary with the service health.

    computer_keys = ('id', 'hostname', 'lastSeenAt', 'threats', 'service_health', 'tamperProtectionEnabled', 'ipv4Addresses', 'associatedPerson', 'health')
    #Add the computers to the computers list
    for all_computers in computers_json["items"]:
        # Make a temporary Dictionary to be added to the sub estate list
        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'] = {'threats': computer_dictionary['health']['threats']['status']}
            computer_dictionary['health'] = {'overall': computer_dictionary['health']['overall']}
            computer_dictionary['service_health'] = {'services': computer_dictionary['health']['services']['status']}
            #computer_dictionary['health'] = {'threats': computer_dictionary['health']['threats']['status']} # seems to work
        computer_list.append(computer_dictionary)
If I comment out this line it works fine

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.
Quote
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#5
Thanks Craig. That is working perfectly now.

Here is the new code

    for all_computers in computers_json["items"]:
        # Make a temporary Dictionary to be added to the sub estate list
        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'}}
Quote
#6
If you just want the values, just assign the values:

for all_computers in computers_json["items"]:
    # Make a temporary Dictionary to be added to the sub estate list
    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)
newbieAuggie2019 likes this post
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#7
Sorry Craig. I understand what you are saying, but I am clear on how to do that.
Quote
#8
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#9
Thanks Craig, I will take a look. Sorry, I thought you were giving me a hint via my code :)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  access dictionary with keys from another and write values to list redminote4dd 6 420 Jun-03-2020, 05:20 PM
Last Post: DeaD_EyE
  Drop Keys From Dictionary donnertrud 8 380 May-30-2020, 11:39 AM
Last Post: DeaD_EyE
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 339 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  Checking if the combination of two keys is in a dictionary? mrsenorchuck 6 683 Dec-04-2019, 10:35 AM
Last Post: mrsenorchuck
  Retrieving items from JSON bazcurtis 12 985 Oct-27-2019, 05:18 PM
Last Post: bazcurtis
  Trouble retrieving dictionary from mysql.connector cursor swechsler 2 639 Sep-17-2019, 05:21 PM
Last Post: swechsler
  retrieving pvalue from statsmodels results Staph 4 549 Jul-18-2019, 03:27 PM
Last Post: Gribouillis
  json.dumps to keep dictionary keys batchenr 1 469 May-14-2019, 11:17 AM
Last Post: buran
  Reference new dictionary keys with a variable slouw 4 603 May-07-2019, 03:30 AM
Last Post: slouw
  Get specific key from multiple keys in python dictionary pradeepkumarbe 0 592 Mar-24-2019, 07:23 PM
Last Post: pradeepkumarbe

Forum Jump:


Users browsing this thread: 1 Guest(s)