Home > OS >  How to find and replace a part of a value in json file in python by taking a function result as an a
How to find and replace a part of a value in json file in python by taking a function result as an a

Time:10-29

The output of the function I run is in this format int which is'2010-10'. I should replace this number in json file which is in the format

                        "x":{..},
                        "y":{..},
                        "z":{
                           "zz":{
                             "test1": "2010-11",
                             "test2": "somestring",
                         },

json file path = '~/example/sample.json'

how do i search and replace the test1 part with the output of the function result i get?

CodePudding user response:

Assuming I understand the question, you should be able to use the keys in this dictionary/json object: obj['z']['zz']['test1'] = "2010-10"

obj pertains to the variable name of the object.

CodePudding user response:

What the question is asking for is a 5 step process:

  1. create a (properly formed) dict.
  2. create a function that returns something.
  3. call the function.
  4. change the value of a specific key.
  5. write the new json string.

This is code that will do such:

import json

def some_function():
    return 'hello world'

# create a dict
d =  {  "x":{'a':'b'},
        "y":{'a':'b'},
        "z":{"zz":{
                    "test1": "2010-11",
                    "test2": "somestring",
                }
        }
}

# call the function
x = some_function()

# change value of a specific key
d['z']['zz']['test1'] = x


# write the new json
j = json.dumps(d, indent=4)
print(j)

Here is the result:

{
    "x": {
        "a": "b"
    },
    "y": {
        "a": "b"
    },
    "z": {
        "zz": {
            "test1": "hello world",
            "test2": "somestring"
        }
    }
}

CodePudding user response:

The answer from @D.L. is correct, so to assign the function result to the dictionary, just assign it like a normal string or variable value.

# pretend I read data from a file, now I have a dictionary
data = {
    "x": {},
    "y": {},
    "z": {
        "zz": {
            "test1": "2010-11",
            "test2": "somestring",
        },
    },
}

# function returns the val in 'test1' by just running a string replace
# I don't know what your function does, but the 
# point is the return value: newval - which is then used directly where you
# called it
def get_results(val):
    newval = ''
    newval = val.replace('-11', '-10')
    return newval


for k,v in data.items():
    if k == 'z':
        for k2, v2 in data[k].items():
            if 'test1' in data[k][k2].keys():
                # print(data[k][k2]['test1'])
                curval = data[k][k2]['test1']
                newval = get_results(curval)  
                data[k][k2]['test1'] = newval  # the return value

You could use the much cleaner code from @D.L. and then assign the updated value.

  • Related