Home > Enterprise >  R studio tweet returning null
R studio tweet returning null

Time:04-30

Following is my Rstudio code: Code:

install.packages("rjson")
library("rjson")
getwd()
result = fromJSON(file = "tweets_no_duplicates_formatted_trying.json")
result$tweet

But result$tweet is returning null instead of tweet, not sure why. How can I get tweet text as definitely tweet is not null.

Json file:

[ 
{
        "tweet": "xyz",
       
        "created_at": "06-04-2022, 07:49:10",
       
        "entities": {
            "hashtags": [
                {
                    "start": 29,
                    "end": 40,
                    "tag": "PrayerRoom"
                },
                {
                    "start": 41,
                    "end": 48,
                    "tag": "auspol"
                }
            ]
        },
        "source": "Twitter for iPhone",
        "public_metrics": {
            "retweet_count": 2,
            "reply_count": 0,
            "like_count": 6,
            "quote_count": 0
        },
        "attachments": null,
        "geo": null
    }
]

CodePudding user response:

It's nested one level deeper:

str(result)
List of 1
 $ :List of 7
  ..$ tweet         : chr "xyz"
  ..$ created_at    : chr "06-04-2022, 07:49:10"
  ..$ entities      :List of 1
  .. ..$ hashtags:List of 2
  .. .. ..$ :List of 3
  .. .. .. ..$ start: num 29
  .. .. .. ..$ end  : num 40
  .. .. .. ..$ tag  : chr "PrayerRoom"
  .. .. ..$ :List of 3
  .. .. .. ..$ start: num 41
  .. .. .. ..$ end  : num 48
  .. .. .. ..$ tag  : chr "auspol"
  ..$ source        : chr "Twitter for iPhone"
  ..$ public_metrics:List of 4
  .. ..$ retweet_count: num 2
  .. ..$ reply_count  : num 0
  .. ..$ like_count   : num 6
  .. ..$ quote_count  : num 0
  ..$ attachments   : NULL
  ..$ geo           : NULL

You can get it by first accessing the whole tweet:

result[[1]]$tweet
[1] "xyz"
  • Related