I have a list of JSON dict that is not in valid JSON format.
They are all listed in a text file
"{'name': 'Alice', 'age': 30, 'tasks' [1, 2, 3], 'description': 'writer'}"
"{'name': 'Bob', 'age': 33, 'tasks' [4, 5, 6], 'description': 'runner'}"
"{'name': 'Kelly', 'age': 23, 'tasks' [7, 8, 9], 'description': 'singer'}"
what I would like to have is
{"name": "Alice", "age": 30, "tasks" [1, 2, 3], "description": "writer"}
{"name": "Bob", "age": 33, "tasks" [4, 5, 6], "description": "runner"}
{"name": "Kelly", "age": 23, "tasks" [7, 8, 9], "description": "singer"}
to have a valid JSON
3
Answers
I use replaceAll() for this purpose
i think the code like this
You can use regex to replace the " (opening double quotes) with { and " (closing double quotes) with } and then use replace to replace all the single quotes with double quotes and then use
json.loads(some_str)
to convert it to JSON dict and you can append it to a new list.