0

Using the package rtweet, I have streamed some tweets and saved them in a JSON file.

When using the following: tweets_df <- parse_stream('file.json'), I get the following error during the process:

enter image description here

Does anyone have any idea how to fix this so that the JSON file can be read into R as a data frame?

  • The apparent problem is that `rtweet` returned invalid JSON. I suggest you file a bug report, and add what is missing here: reproducibility. – r2evans Jan 06 '21 at 13:26
  • Welcome to SO, HelpNeeded4! Questions on SO (especially in R) do much better if they are reproducible and self-contained. By that I mean including attempted code (please be explicit about non-base packages), sample representative data (perhaps via `dput(head(x))` or building data programmatically (e.g., `data.frame(...)`), possibly stochastically after `set.seed(1)`), perhaps actual output (with verbatim errors/warnings) versus intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Jan 06 '21 at 13:30
  • Additionally, please do not post an image of code/data/errors: it cannot be copied or searched (SEO), it breaks screen-readers, and it may not fit well on some mobile devices. Ref: https://meta.stackoverflow.com/a/285557 (and https://xkcd.com/2116/). – r2evans Jan 06 '21 at 13:30

1 Answers1

0

Have you tried it this way? I don't personally use rtweet but work with json files.

#load library to read json
library(jsonlite)
json_data <- fromJSON("db.json")

It reads it as a nested list but then you can simply change it to a dataframe using df<-rlist::list.stack(x, fill=TRUE )'

You might have to adapt it and for example use a loop if your json file contains several users.

Lara
  • 5
  • 5
  • How does this work for you? If you use `rtweet`, then please make this answer somewhat reproducible (much more than the question). If you are not using `rtweet`, then I'd be curious how you know that `fromJSON` will work with data we don't currently have. – r2evans Jan 06 '21 at 13:27
  • Thanks, sorry for deleting it. I already solved it. But will keep this as an alternative. – Lara Jan 06 '21 at 13:30