I have to perform some tests to tune some numeric parameters of a json file. To make simple, I replaced all these values by the string 'variable', then did the following:
numeric_vals = [10,20, 30, 40] # numeric values to replace in that order
with open ('mypath') as my_file:
json_str = my_file.read()
for i in numeric_vals:
json_str = json_str.replace("\"variable\"", str(i), 1)
c = json.loads(json_str) #loading in order to work with
This works fine, but is there a more efficient way to do this ? Values that need to be replaced are in variable depths, and may be inside lists etc. My json file is 15Kb and I need to test many (really many!) configurations. At each test, about 200 variables need to be replaced. I am on python 2.7 , but python 3.5 is also an option. Thanks for your help!
EDIT :
here is a sample of my dict. It should be noted that the real thing is far longer and deeper:
{
"1": {
"transition": {
"value": "variable", # edit here
"unite": "sec"
},
"sequence": [
{
"step": "STEP",
"name": "abc",
"transition": {
"value": "variable", #edit here
"unite": "sec"
},
"entity": "aa",
"_equipement": 3,
"_value": 0
},
{
"step": "FORK",
"BRANCHES": [
{
"": {
"sequence": [
{
"step": "STEP",
"name": "klm",
"transition": {
"value": "variable", # edit here
"unite": "sec"
},
"entity": "vvv",
"_equipement": 10,
"_value": 0,
"conditions": [
[
{
"name": "ddd",
"type": "el",
"_equipement": 7,
"_value": 1
}
]
]
}
]
}
},
{
"SP": {
"sequence": [
{
"step": "STEP",
"name": "bbb",
"transition": {
"value": "variable", # edit here
"unite": "sec"
},
"entity": "bb",
"_equipement": 123,
"_value": 0,
"conditions": [
[
{
"name": "abcd",
"entity": "dgf",
"type": "lop",
"_equipement": 7,
"_valeur": 0
}
]
]
}
]
}
}
]
}
]
}
}