I have a very large JSON-like file, but it is not using proper JSON syntax: the object keys are not quoted. I'd like to write a script to fix the file, so that I can load it with json.loads
.
I need to match all words followed by a colon and replace them with the quoted word. I think the regex is w+s*:
and that I should use re.sub
, but I'm not exactly sure how to do it.
How can I take the following input and get the given output?
# In
{abc : "xyz", cde : {}, fgh : ["hfz"]}
# Out
{"abc" : "xyz", "cde" : {}, "fgh" : ["hfz"]}
# In
{
a: "b",
b: {
c: "d",
d: []
},
e: "f"
}
# Out
{
"a": "b",
"b": {
"c": "d",
"d": []
},
"e": "f"
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…