How can I edit/rename keys during json.load in python? How can I edit/rename keys during json.load in python? mongodb mongodb

How can I edit/rename keys during json.load in python?


You almost had it:

import jsondef remove_dot_key(obj):    for key in obj.keys():        new_key = key.replace(".","")        if new_key != key:            obj[new_key] = obj[key]            del obj[key]    return objnew_json = json.loads(data, object_hook=remove_dot_key) 

You were returning a dictionary inside your loop, so you'd only modify one key. And you don't need to make a copy of the values, just rename the keys.