Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Updating Key in JSON File
#1
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?
Reply


Messages In This Thread
Updating Key in JSON File - by decampo04 - Apr-21-2019, 09:48 AM
RE: Updating Key in JSON File - by Larz60+ - Apr-21-2019, 05:11 PM
RE: Updating Key in JSON File - by decampo04 - Apr-22-2019, 09:00 AM
RE: Updating Key in JSON File - by rlgoodman - Apr-22-2019, 03:20 PM
RE: Updating Key in JSON File - by decampo04 - Apr-22-2019, 04:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 273 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  Python openyxl not updating Excel file MrBean12 1 374 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  parse json field from csv file lebossejames 4 796 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Updating sharepoint excel file odd results cubangt 1 899 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Python Script to convert Json to CSV file chvsnarayana 8 2,598 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,197 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,683 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  validate large json file with millions of records in batches herobpv 3 1,310 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Writing to json file ebolisa 1 1,033 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Trying to parse only 3 key values from json file cubangt 8 3,548 Jul-16-2022, 02:05 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020