0

So i have a Dict like so:

{
  "environment": [
    {
      "appliances": [
        {
          "services-status": "GOOD",
          "ping-status": "REACHABLE"
        },
        {
          "softwareVersion": "16.1-R1-S2 50b46b5 20170829",
          "services-status": "GOOD",
          "ping-status": "REACHABLE"
        }
      ],
      "vd_url": "https://bla1"
    },
    {
      "appliances": [
        {
          "ipAddress": "10.4.64.108"
          "type": "branch",
          "sync-status": "IN_SYNC"
        },
        {
          "services-status": "GOOD",
          "ping-status": "REACHABLE"
          "sync-status": "IN_SYNC"
        }
      ],
      "vd_url": "https://bla2"
    },
  ],
  "failed_urls": [
    "https://gslburl",
    "https://gslburl",
    "https://localhost",
    "https://localhost",
    "https://localhost"
  ]
}

or it can be something like this

{
  "softwareVersion": "16.1-R1-S2 50b46b5 20170829",
  "services-status": "GOOD",
  "ping-status": "REACHABLE"
  "vd_url" : "https://blah3"
}

or it can be something like this

{
  "environment": [
    {
      "appliances": [
        {
          "services-status": "GOOD",
          "ping-status": "REACHABLE"
        },
        {
          "softwareVersion": "16.1-R1-S2 50b46b5 20170829",
          "services-status": "GOOD",
          "ping-status": "REACHABLE"
        }
      ],
      "vd_url": "https://bla1"
    }
  ]
}

or it can be in any combination of dictionaries possible, but what i am trying to achieve is write a generic function that can take the dictionary, replace a key with another value and return the new dict. Just a pseudo code:

def replace(dict_obj, key, new_value)
    for dict in dict_obj:
        for k, v in dict.items()
        if k == key:
              dict[k] = new_value

    return dict_obj

but in the end i want the same dictionary object - dict_obj that i passed but with the new values and it should work for any type of dicts above Scratching my head as to how to solve it :(

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Wilbur Dsouza
  • 127
  • 1
  • 10
  • 1
    Start by finding the key... – Mad Physicist Mar 28 '18 at 04:02
  • If you `return` the dict, you usually don't need—or even want—it to be the same thing you passed in. On the other hand, if you modify the dict in-place, you usually don't need—or want—to return it. (That's why builtin methods like `list.sort` and `dict.update` and similar mutating functions in the stdlib almost always return `None`.) – abarnert Mar 28 '18 at 04:35

1 Answers1

1

You can use recursion to determine if value from a key,value pair is a list or a dict and then iterate accordingly.

def replace(dict_obj, key, new_value):
    for k,v in dict_obj.iteritems():
        if isinstance(v,dict):
            # replace key in dict
            replace(v, key, new_value)
        if isinstance(v,list):
            # iterate through list
            for item in v:
                if isinstance(item,dict):
                    replace(item, key, new_value)
        if k == key:
            dict_obj[k] = new_value
Shivam Singh
  • 1,584
  • 1
  • 10
  • 9