I'm facing some difficulties while trying to figure it out, what it the optimal way to convert a string to a valid JSON, using Python.
Basically, what I need to do is to read a string (which is already given in a JSON format) from a file, and convert it to a valid JSON. The only problem in my case, is the " , " after the last object:
{
"InputTable" :
[
{
"ServerName":"serverOne",
"To":"userOne",
"CC":"",
"TemplateName":"LinuxTemplate"
},
{
"ServerName":"serverTwo",
"To":"userTwo",
"CC":"userFive",
"TemplateName":"LinuxTemplateWithCC"
}, << get rid of this comma
],
"Params":
[
{"Col_0":"Server","Col_1":"User","Col_2":"Action"}
]
}
This is how I read the file:
with open('/nfs/somePath/LinuxInput.JSON') as json_file:
try:
jsonFormatInputTable = json.load(json_file)
except:
print ("Couldn't read JSON file, cancled operation.")
return
Loading the JSON file fails because the JSON is not valid. Do you have any suggestion, first how to get rid of that last comma, and second how to validate the string and make sure it is in the correct format?
ast.literal_eval()