Write a json file from list
Question:
I have the following list data I want to save in a json file to be access later:
data = [{"nomineesWidgetModel":{"title":"","description":"",
"refMarker":"ev_nom","eventEditionSummary":{"awards":[{"awardName":"Oscar","trivia":[]}]}}}]
If saved as txt:
for item in data:
with open('./data/awards.txt', 'w', encoding='utf-8') as f:
f.write(', '.join(str(item) for item in data))
Output:
{"nomineesWidgetModel":{"title":"","description":"","refMarker":"ev_nom",
"eventEditionSummary":{"awards":[{"awardName":"Oscar","trivia":[]}]}}}
But I get an error when opening the file later in Jupyter Notebook
If save as json
for item in data:
with open('data.json', 'w', encoding='utf-8') as f:
json.dump(item, f, ensure_ascii=False, indent=4)
Output with extra backslash:
"{"nomineesWidgetModel":{"title":"","description":"","refMarker":"ev_nom",
"eventEditionSummary":{"awards":[{"awardName":"Oscar","trivia":[],}]}}
Is there a simpler way to do this without having to import the file and replace the extra slashes?
Answers:
Thanks to @Alexander explanation above, I was able to save the content I was scraping in a dict, and not a list, and then save as json while iterating the pages with:
with open('data.json', 'a') as file:
json.dump(data, file, indent=1)
Just use json
as usual:
import json
data = [{"nomineesWidgetModel":{"title":"","description":"", "refMarker":"ev_nom","eventEditionSummary":{"awards":[{"awardName":"Oscar","trivia":[]}]}}}]
with open('data.json', 'w') as f:
json.dump(data, f, indent=4)
I have the following list data I want to save in a json file to be access later:
data = [{"nomineesWidgetModel":{"title":"","description":"",
"refMarker":"ev_nom","eventEditionSummary":{"awards":[{"awardName":"Oscar","trivia":[]}]}}}]
If saved as txt:
for item in data:
with open('./data/awards.txt', 'w', encoding='utf-8') as f:
f.write(', '.join(str(item) for item in data))
Output:
{"nomineesWidgetModel":{"title":"","description":"","refMarker":"ev_nom",
"eventEditionSummary":{"awards":[{"awardName":"Oscar","trivia":[]}]}}}
But I get an error when opening the file later in Jupyter Notebook
If save as json
for item in data:
with open('data.json', 'w', encoding='utf-8') as f:
json.dump(item, f, ensure_ascii=False, indent=4)
Output with extra backslash:
"{"nomineesWidgetModel":{"title":"","description":"","refMarker":"ev_nom",
"eventEditionSummary":{"awards":[{"awardName":"Oscar","trivia":[],}]}}
Is there a simpler way to do this without having to import the file and replace the extra slashes?
Thanks to @Alexander explanation above, I was able to save the content I was scraping in a dict, and not a list, and then save as json while iterating the pages with:
with open('data.json', 'a') as file:
json.dump(data, file, indent=1)
Just use json
as usual:
import json
data = [{"nomineesWidgetModel":{"title":"","description":"", "refMarker":"ev_nom","eventEditionSummary":{"awards":[{"awardName":"Oscar","trivia":[]}]}}}]
with open('data.json', 'w') as f:
json.dump(data, f, indent=4)