Conor Thompson
Conor Thompson

Reputation: 308

Access key value from JSON array of objects Python

I've been researching the past few days on how to achieve this, to no avail.

I have a JSON file with a large array of json objects like so:

[{
    "tweet": "@SHendersonFreep @realDonaldTrump watch your portfolios go to the Caribbean banks and on to Switzerland. Speculation without regulation",
    "user": "DGregsonRN"
},{
    "tweet": "RT @CodeAud: James Mattis Vs Iran.\n\"The appointment of Mattis by @realDonaldTrump got the Iranian military leaders' more attention\". https:\u2026",
    "user": "American1765"
},{
    "tweet": "@realDonaldTrump the oyou seem to be only fraud I see is you, and seem scared since you want to block the recount???hmm cheater",
    "user": "tgg216"
},{
    "tweet": "RT @realDonaldTrump: @Lord_Sugar Dopey Sugar--because it was open all season long--you can't play golf in the snow, you stupid ass.",
    "user": "grepsalot"
},{
    "tweet": "RT @Prayer4Chandler: @realDonaldTrump Hello Mr. President, would you be willing to meet Chairman #ManHeeLee of #HWPL to discuss the #PeaceT\u2026",
    "user": "harrymalpoy1"
},{
    "tweet": "RT @realDonaldTrump: Thank you Ohio! Together, we made history \u2013 and now, the real work begins. America will start winning again! #AmericaF\u2026",
    "user": "trumpemall"
}]

And I am trying to access each key and value, and write them to a csv file. I believe using json.loads(json.dumps(file)) should work in normal json format, but because there is an array of objects, I can't seem to be able to access each individual one.

converter.py:


    import json
    import csv

    f = open("tweets_load.json",'r')
    y = json.loads(json.dumps(f.read(), separators=(',',':')))
    t = csv.writer(open("test.csv", "wb+"))

    # Write CSV Header, If you dont need that, remove this line
    t.writerow(["tweet", "user"])

    for x in y:
       t.writerow([x[0],x[0]])

grab_tweets.py:


    import tweepy
    import json

    def get_api(cfg):
      auth = tweepy.OAuthHandler(cfg['consumer_key'], cfg['consumer_secret'])
      auth.set_access_token(cfg['access_token'], cfg['access_token_secret'])
      return tweepy.API(auth)

    def main():

      cfg = {
        "consumer_key"        : "xxx",
        "consumer_secret"     : "xxx",
        "access_token"        : "xxx",
        "access_token_secret" : "xxx"
        }
      api = get_api(cfg)
      json_ret = tweepy.Cursor(api.search, q="@realDonaldTrump",count="100").items(100)
      restapi =""
      for tweet in json_ret:
          rest = json.dumps({'tweet' : tweet.text,'user' :str(tweet.user.screen_name)},sort_keys=True,indent=4,separators=(',',': '))
          restapi = restapi+str(rest)+","
      f = open("tweets.json",'a')
      f.write(str(restapi))
      f.close()

    if __name__ == "__main__":
      main()

The output so far is looking like:

tweet,user^M
{,{^M
"
","
"^M
 , ^M
 , ^M
 , ^M
 , ^M
"""",""""^M
t,t^M
w,w^M
e,e^M
e,e^M
t,t^M
"""",""""^M
:,:^M
 , ^M
"""",""""^M
R,R^M
T,T^M
 , ^M
@,@^M
r,r^M
e,e^M
a,a^M
l,l^M
D,D^M
o,o^M
n,n^M
a,a^M
l,l^M

What exactly am I doing wrong?

Upvotes: 0

Views: 1769

Answers (1)

Conor Thompson
Conor Thompson

Reputation: 308

turns out it was the json.dumps(), should've read more into what it does! Thanks.

Upvotes: 0

Related Questions