Iterate through every key value in JSON dictionary in Python
<p>I have a JSON string:</p>

<pre><code>{"emailId":[],"Age":[],"siblings":[],"addedGroupIds":[],"role":{"name":"x","description":"y","Permissions":[],"clientId":102}}
</code></pre>

<p>I want to replace value from every key-value pair(unless the value itself is a dictionary,eg:"role"). I converted the above mentioned string to a dictionary in somerandomFunc() and then sent this dictionary to iterator(). Iterator()  will recursively check if the value in each key-value pair is a dictionary or not. If the value is not a dictionary, it will send the key to smartScanner() where I will be replacing the value of the recieved key with myTestString</p>

<pre><code>def somerandomfunc(self,JSONString):
    payload=simplejson.loads(JSONString)
    self.payload=payload
    iterator(payload)

def iterator(self,payload):
    for i in payload:
        if isinstance(payload[i],dict):
            self.iterator(payload[i])
        else:
            self.smartScanner(i)

def smartScanner(self,i):
    mytestString='"&gt;qqqq'
    if (self.payload.has_key(i)):
        self.payload[i]=mytestString
    print "%s:%s"%(i,self.payload.has_key(i))
</code></pre>

<p>I get the following output:</p>

<pre><code>existingGroupIds:True
existingUserIds:True
assetPermissions:False
name:False
clientId:False 
description:False
removedGroupIds:True
removedUserIds:True
addedGroupIds:True
addedUserIds:True
</code></pre>

<p>Which means that if statement in smartScanner() failed for keys "assetPermissions","name","clientId" and "description". And I wasn't able to replace the values of these keys with myteststring. I can understand the reason it failed was because it tried to access payload["name"] instead of payload["role"]["name"]</p>

<p>How do I change my logic so that I be able to replace the values even for keys "assetPermissions","name","clientId" and "description"? And that my program does not break no matter any hierarchies of dictionary are present in the JSON payload</p>