Convert Json String in Text file to Json file with Python 3

Viewed 114

I have gathered around 5000 tweets using Twitter API but mistakenly wrote them into a .txt. I need them to be in .json format instead. I tried several solutions from simply changing the file format from txt to json to 3 hours worth of stackoverflow answers such as this:

import json

f = open("tweets.txt", "r")
content = f.read()
#print(content)

text_file = open("tweets4.json", "w")
text_file.write(content)
text_file.close()

But even this opened like a text string. I also tried to use json.dump() but since the string is already in Json format, it did double encoding and it was full of "\

In short, it looks like this: Json string in text format

But should look like this (I re ran the code and saved new tweets to a json): Json format

0 Answers
Related