I am trying to update the value in the JSON file, after the JSON dump python adds the additional "" in value. which I am trying to remove. Can you someone help.
raw JSON file data
{
"ProjectBranch": "Dev",
"BaselineRestore": {
"releaseName": "02",
"useProcessedBaseline": false,
"incrementalRun": false,
"breakPipelinePostRestore": false,
"releaseYear": "2022",
"debugMode": true
}
}
my python script
import os
import glob
import json
import shutil
import re
cwd = os.getcwd()
pipeline_config_file = os.path.join(cwd, "config", "test.json")
with open(pipeline_config_file, "r ") as f:
data = json.load(f)
incrementalRun = str(data['BaselineRestore']['incrementalRun']).lower()
data['BaselineRestore']['incrementalRun'] = 'true'
data['BaselineRestore']['incrementalRun'] = data['BaselineRestore']['incrementalRun'].strip()
#data = data.replace('"true"', 'true')
with open(pipeline_config_file, 'w') as f:
json.dump(data, f, indent=4)
script output
{
"ProjectBranch": "Dev",
"BaselineRestore": {
"releaseName": "02",
"useProcessedBaseline": false,
"incrementalRun": "true",
"breakPipelinePostRestore": false,
"releaseYear": "2022",
"debugMode": true
}
Expected Data
{
"ProjectBranch": "Dev",
"BaselineRestore": {
"releaseName": "02",
"useProcessedBaseline": false,
"incrementalRun": true,
"breakPipelinePostRestore": true,
"releaseYear": "2022",
"debugMode": true
}
}
CodePudding user response:
Your line data['BaselineRestore']['incrementalRun'] = 'true' places a string 'true' here, as opposed to the True boolean. This is what causes the difference between the two.
You should use:
import os
import glob
import json
import shutil
import re
cwd = os.getcwd()
pipeline_config_file = os.path.join(cwd, "config", "test.json")
with open(pipeline_config_file, "r ") as f:
data = json.load(f)
data['BaselineRestore']['incrementalRun'] = True
with open(pipeline_config_file, 'w') as f:
json.dump(data, f, indent=4)
Assuming that your goal is to exclusively update false to true for incrementalRun.
