I have a script which processes data returned from a web API. The data returned is a string representation of a list of dictionaries.
I know about ast.literal_eval() to convert the string representation of the dictionaries into real ones.
The data looks like this (Reminder: those are strings, not dicts):
new = [{"id": "L0", "tags": null, "disable": false},
{"id": "L1", "tags": null, "disable": false},
{"id": "L2", "tags": null, "disable": false}]
Because 'null' and 'false' are not enclosed in quotes, ast.literal_eval() fails with an error. If I manually add quotes to null and false above, it works perfectly.
I was trying to iterate through the strings and modify the substrings null and false to "null" and "false" with this, but it does nothing:
for i in new:
i.replace("null", "\"null\"")
i.replace("false", "\"false\"")
How do I modify those substrings to add enclosing quotes? If possible I would prefer to modify the existing list (new[]) instead of creating a new one, but if I have to create a new one to get it to work, then that's what I will do.