2

I have a JSON file, for example:

{
    "items": {
        "item1": {
            "thing1": "SomeValue", 
            "thing2": "AnotherValue"
        }, 
        "item2": {
            "thing1": "SomeValue", 
            "thing2": "AnotherValue"
        },
        "item3": {
            "thing1": {
                "moreStuff": "someStuff",
                "evenMoreStuff": "someMoreStuff"
            }
        }
    }
} 

I want to make a generic function to update a single value in the file by passing a list of strings as keys.

def update_dict(value, keys):
    with open(somePath, "r") as f:
        data = json.load(f)
    
    data[keys[0]][keys[1]][keys[2]] = value

    with open(somePath, "w") as f:
        json.dump(data, f,

value = "AThirdValue"
keys = ["items", "item2", "thing1"]

update_dict(value, keys)

What I can't figure out is how this can be done if one does not know the length of the keys list. For example this would not work:

value = "AThirdValue"
keys = ["items", "item3", "thing1", "moreStuff"]
update_dict(value, keys)

I would not want to use if-statements to check the length and then having to edit this function if I add another level of depth.

3 Answers 3

1

You can use a loop to simulate tree-walking:

def update_dict(value, keys):
    with open(somePath, "r") as f:
        data = json.load(f)

    point = data
    last_key = keys.pop()
    for key in keys:
        point = point[key]
    
    point[last_key] = value

    with open(somePath, "w") as f:
        json.dump(data, f)
Sign up to request clarification or add additional context in comments.

Comments

1

you could cascade the items of the keys list like these

import json

def update_dict(value, keys):
    with open(somePath, "r") as f:
        data = json.load(f)
    
    tmp_data = data
    for key in keys[:-1]:
        tmp_data = tmp_data[key]
    tmp_data[keys[-1]] = value 
    with open(somePath, "w") as f:
        json.dump(data, f)

value = "AThirdValue"
keys = ["items", "item2", "thing1"]

update_dict(value, keys)

other way to code it

import json

def update_dict(value, *keys):
    with open(somePath, "r") as f:
        data = json.load(f)
    
    tmp_data = data
    for key in keys[:-1]:
        tmp_data = tmp_data[key]
    tmp_data
    tmp_data[keys[-1]] = value 
    with open(somePath, "w") as f:
        json.dump(data, f)

value = "AThirdValue"
update_dict(value, "items", "item2", "thing1")

Comments

0

Use variable number of arguments.

def functionName(*argument)

Replace:

data[keys[0]][keys[1]][keys[2]] = value

with:

def update_dict(value, *keys):
    with open(somePath, "r") as f:
        data = json.load(f)

    # data[keys[0]][keys[1]][keys[2]] = value
    for key in keys:
        data[key] = value

    with open(somePath, "w") as f:
        json.dump(data, f)

value="AThirdValue"
keys = ["items", "item2", "thing1"]

update_dict(value, keys)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.