I have a json file named test.json and I need a python function for substitute some specific values in the same file. The json file is:
[
{
"ParameterKey" : "HOME",
"ParameterValue" : "/home"
},
{
"ParameterKey" : "Shell",
"ParameterValue" : "/bin/sh"
},
{
"ParameterKey": "EnvHost",
"ParameterValue": "localhost"
},
{
"ParameterKey": "EnvUser",
"ParameterValue": "stall"
},
{
"ParameterKey": "Type",
"ParameterValue": "super"
}
]
I need to replace only "Shell" and "Type" to other values but I'm struggling to do so.
You can do something like this.
import json
with open('/path/to/josn_file.json', 'r') as file:
json_data = json.load(file)
for item in json_data:
if item['ParameterKey'] in ["Shell","Type"]:
item['ParameterKey'] = "new value"
with open('/path/to/josn_file.json', 'w') as file:
json.dump(json_data, file, indent=2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With