Apr-21-2019, 09:48 AM
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:
Output:{
"operations": {
"ops_modify": {
"paths": [
"ops_set_config"
],
"methods": [
"GET"
]
},
"ops_read": {
"paths": [
"ops_get_summary",
"ops_get_request_info"
],
"methods": [
"GET"
]
}
},
"roles": {
"Server Op": [
"emc",
"mgmt",
"fips_read"
],
"Deployer": [
"mgmt",
"config"
],
"KP Admin": [
"mgmt",
"kp"
]
},
"permissions": {
"logs": [
"ops_read",
"file"
],
"adminusers": [
"adminusers.read"
]
}
}
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?