Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Updating Key in JSON File
#2
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:
Output:
Dictionary before modification 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'] Dictionary after modification 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: ['CN=Dev_Deployer', 'OU=test', 'DC=example', 'DC=com'] KP Admin: ['mgmt', 'kp'] permissions logs: ['ops_read', 'file'] adminusers: ['adminusers.read']
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 288 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  Python openyxl not updating Excel file MrBean12 1 391 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  parse json field from csv file lebossejames 4 801 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Updating sharepoint excel file odd results cubangt 1 912 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Python Script to convert Json to CSV file chvsnarayana 8 2,609 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,217 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,734 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  validate large json file with millions of records in batches herobpv 3 1,323 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Writing to json file ebolisa 1 1,044 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Trying to parse only 3 key values from json file cubangt 8 3,553 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