I have a Python-script that makes an File with invalid JSON. Now I want to manipulate this JSON-File so it becomes a valid JSON-file by adding a comma between every object, at the beginning of the File a '[' and at the end a ']'. Is there a way to make this with JSON alone or do i have to find a way with other read and write functions?
Exsample_File.json:
{
"firstName": "Bidhan",
"lastName": "Chatterjee",
"age": 40,
"email":"bidhan@example.com"
}
{
"firstName": "hanbid",
"lastName": "jeeChatter",
"age": 10,
"email":"example@bidhan.com"
}
....
n times
New_File.json:
[
{
"firstName": "Bidhan",
"lastName": "Chatterjee",
"age": 40,
"email":"bidhan@example.com"
},
{
"firstName": "hanbid",
"lastName": "jeeChatter",
"age": 10,
"email":"example@bidhan.com"
},
....
n times
]
This is the function that makes this JSON-File. I dont want to touch the other code where the str is generated.
data = json.loads(str)
with open('Example_File.json','ab')as outfile:
json.dump(data, outfile, indent=2)
So far i dont have an idea to solve this problem. so there is no code sample that would help.
The result should be like the New-File