a={ "asdf" : [{"yuyu": "okkojcoa"}] "efg": [{"password": "password"} } a - asdf - yuyu -efg - password DFS def encodePassword(reqJson, fields): allPaths=[] for key in reqJson.getKeys(): getPath(reqJson,key,keyfields,allPaths) resJson={} tempJson=resJson for path in allPaths: tempJson=reqJson keyQueue=[key for key in reqJson.getKeys()] while len(keyQueue)>0: currKey = keyQueue.pop(0) if currKey == path[0]: tempJson[path[0]] = getDecodedInput(tempJson[currKey]); break else: tempJson[currKey]= z x c ['a']['b']['c'] n r u i def getPath(jsonIn,key,fields,currPath,allPaths): currPath.append(key) if (key in fields): allPath.add(currPath) return for tempKey in reqJson.getKeys(): getPath(reqJson[key],tempKey,fields,currPath,allPaths) currPath.pop() a["asdf"]["password"] <-------------------> ["password", "aadhar"] { "asdf" : [{"password": "[REDACTED]"}]}