Hi,
I have multiple files which I need to format them to be one JSON file:
Here is an example of a file which I have:
{"t": "test", "title": "test", "content": "test"}
{"t": "test2", "title": "test", "content": "test2"}
What I need is to be like:
[
{"t": "test", "title": "test", "content": "test"},
{"t": "test2", "title": "test2", "content": "test2"}
]
It will look like this in the end (Note that the below JSON similar to the Above, second, one):
[{
"t": "test",
"title": "test",
"content": "test"
},
{
"t": "test2",
"title": "testw",
"content": "test2"
}
]
What I have tried:
I have the below python code:
import io
import os
import json
def wrap_files_in_dir(dirname):
data = {}
list_of_reviews = []
for filename in os.listdir(dirname):
file_path = os.path.join(dirname, filename)
if os.path.isfile(file_path):
with io.open(file_path, 'r', encoding='utf-8', errors='ignore') as rfile:
contents = rfile.read()
list_of_reviews.append(contents)
with io.open('AppStoreReviews.json', 'w', encoding='utf-8' , errors='ignore') as wfile:
data["reviews"] = list_of_reviews
wfile.write(unicode(json.dumps(data, ensure_ascii=False)))
if __name__ == '__main__':
wrap_files_in_dir('/Users/Jack/PycharmProjects/MyProject')
print("Your Reviews marged and converted to JSON")
I know that I'm missing some code here which enter to each file in my directory.. or could it be something else?
Can someone help me with this?
the problem is that the code does create me a JSON file, BUT, the JSON is without any arrays and doesn't have any "," between my objects.