Updating Key in JSON File - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Updating Key in JSON File (/thread-17710.html) |
Updating Key in JSON File - decampo04 - Apr-21-2019 Hi all, this is my first post as I am a Python beginner and would greatly appreciate any help. Basically, I need to update a a JSON file used by a service that contains a simple JSON list. However, rather than the values I need to replace the key names so the roles map to an Active Directory group within the sample file below: So, I need to replace the 'Deployer' role with an AD distinguished name such as 'CN=Dev_Deployer,OU=test,DC=example,DC=com'. I've already updated another JSON file using the json module but in that case I only needed to change the values against a key, not the key itself. I have managed to achieve the above by using the code below:def replace_acls(self, filename, dn, role): with open(filename, 'r') as f: dn = f.read().replace(role, dn) with open(filename, 'w') as f: f.write(dn)However, I'd like to follow best practice of using the JSON module instead but cannot get it to write the data back to the file. As an example of the code I cannot get to work: import json with open('acl.json', 'r') as f: data = json.load(f) for item in data['roles']: item = item.replace('Deployer', 'CN=Dev_Deployer,OU=test,DC=example,DC=com') with open('acl.json', 'w') as f: json.dump(data, f, indent=2)Any ideas, thanks in advance? RE: Updating Key in JSON File - Larz60+ - Apr-21-2019 I added code to print out dictionary before and after changes, also cleaned up code a bit ** Note ** Uses f-string which requires python 3.6 or newer import json import os def replace_acls(self, filename, dn, role): with open(filename, 'r') as f: dn = f.read().replace(role, dn) with open(filename, 'w') as f: f.write(dn) def display_dict(dictname, level=0): ''' Display formatted dictionary ''' indent = " " * (4 * level) for key, value in dictname.items(): if isinstance(value, dict): print(f'\n{indent}{key}') level += 1 display_dict(value, level) else: print(f'{indent}{key}: {value}') if level > 0: level -= 1 def main(): # anchor starting directory os.chdir(os.path.abspath(os.path.dirname(__file__))) with open('acl.json', 'r') as f: data = json.load(f) print(f'\nDictionary before modification', end='') display_dict(data) data['roles']['Deployer'] = ['CN=Dev_Deployer','OU=test','DC=example','DC=com'] print(f'\nDictionary after modification', end='') display_dict(data) with open('acl.json', 'w') as f: json.dump(data, f, indent=2) if __name__ == '__main__': main()Output:
RE: Updating Key in JSON File - decampo04 - Apr-22-2019 Hi Larz60+ thanks for your post, really appreciate the response. However, that's not quite what I was after as it actually needs to make the following changes to the file: So 'Deployer' itself changes to the distinguished name of 'CN=Dev_Deployer,OU=test,DC=example,DC=com'.
RE: Updating Key in JSON File - rlgoodman - Apr-22-2019 You can't replace a key in a dictionary. You have to create a new entry in the dictionary with your new key, with the same contents as the old item. Then delete the old key/item if that's what is required. RE: Updating Key in JSON File - decampo04 - Apr-22-2019 Thanks for confirming. |