'How to convert a string made of list of objects to json
I am working with flask and I use Tweepy to get some tweets sent to the static folder and manipulate the result with it.
The problem I encounter it returns a string which is an array of objects. If I use JSON.parse() is going to give me an error such as unexpected token ‘ in JSON at position 2 in JSON.parse
[{1:a, name:name, etc...}, {1:a, name:name, etc...}, {1:a, name:name, etc...}, {1:a, name:name, etc...}, {1:a, name:name, etc...},]
I got this string from:
twitter_search.py
...
for tweet in tweets:
# data.append({"tweet": tweet.full_text})
data.append({"time": str(tweet.created_at), "user" : str(tweet.user.screen_name), "tweet": str(tweet.full_text)})
...
in app.py
@app.route('/')
def index():
ip = supp.get_data('https://freegeoip.app/json/')
iptojson = json.loads(ip.content)
sctojson = ip.sc
data = twitter_search.twsejs()
s1 = json.dumps(data)
d2 = json.loads(s1)
return render_template('index.html',data=d2, iptojson = iptojson, sctojson = sctojson)
But it is a string and I need to use it as a JSON what can I do? I tried everything JSON.dumps dump parse can't figure it out
This is an example of a get req
[{'time': '2022-03-16 19:24:07+00:00', 'user': 'AnnBrock432', 'tweet': 'Yes, I\'m primarily on here for GAWs. Not because I\'m a "bad person" I\'m frozen w/ anxiety over possible homelessness. I\'d like to be in the mindset to socialize, but even in real time it\'s hard rn. Depression is real. If I had a solution to pressing matters it would change things'}, {'time': '2022-03-16 19:24:04+00:00', 'user': 'SiameseCatsMom', 'tweet': 'I must do something\nDepression and back pain has me just like a slug in bed'}, {'time': '2022-03-16 19:24:02+00:00', 'user': 'TheCrabyMermade', 'tweet': 'My depression is so bad that even my two mile walk in the morning doesn’t give me nearly as much of the feel good hormones that everyone raves about. I just feel slightly madder bcuz here I am working out & not feeling the effects so why am I even walking 2 miles on the morning?!'}, {'time': '2022-03-16 19:24:02+00:00', 'user': 'AntiMysterious', 'tweet': "@jorgemarqueza @krisjw2630 @seanhannity Sorry buddy. It doesn't matter. Obama had to deal with the biggest recession since the great depression and all you guys did was cry about his spending. \n\nSo you guys are not only failures but you are hypocrites. Lol."}, {'time': '2022-03-16 19:24:01+00:00', 'user': 'TheDuke256', 'tweet': 'RT @Chiomy_mi: This country is so hard & messed up rn! \nAlot of people out there are going through tough time, hence many have fall into de…'}, {'time': '2022-03-16 19:24:01+00:00', 'user': 'Triphighalways1', 'tweet': 'Transcend the multiverse and easy #depression with #lsd and #shrooms link'}, {'time': '2022-03-16 19:23:59+00:00', 'user': 'i_depress_me', 'tweet': 'One of the upsides of crippling depression is that you occasionally get to have a box of Girl Scout cookies for breakfast.'}, {'time': '2022-03-16 19:23:59+00:00', 'user': '0bviouSquirre1', 'tweet': "in realistic terms it has been two and a half months since the cold weather set in such that i couldn't ride my bike\n\nseasonal depression has me acting like i've been locked inside for six months minimum, good lord"}, {'time': '2022-03-16 19:23:58+00:00', 'user': 'sunshiineo_o', 'tweet': "@OthelloVen that's OK for real 😭😭 idk where i sit but it might be on the depression side of the spectrum"}]
Solution 1:[1]
Making a request to an API comports to receive all sorts of responses. In order to have a well-formatted JSON, all I had to do is use a regex and avoid things such as double quotes, special characters, and brake lines. This made me have a cleaner JSON.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Luke Delray |
