2

I would like to import json data in R. I know some discussions are already posted about this subject, but unfortunately I don't have the result I want with these codes or they have many errors with my file.

I would like to import this: https://api.stocktwits.com/api/2/streams/symbol/AAPL.json in R.

I tried to do that:

AAPLapi <-  'https://api.stocktwits.com/api/2/streams/symbol/AAPL.json'

AAPLapi <- fromJSON(AAPLapi)

AAPLapi <- lapply(AAPLapi, function(x) {
  x[sapply(x, is.null)] <- NA
  unlist(x)
})

table=do.call("rbind", AAPLapi)
View(table)

But it's having an error at the line table=do.call....... Last week I don't know why but the line worked, but I had a table very weird and it was wrong.

Could you help me please?

Ajay Ohri
  • 3,382
  • 3
  • 30
  • 60
Cec Jurczyk
  • 87
  • 1
  • 10
  • Please include the output of `sessionInfo()` in your question (or at least include your OS, and the versions of the curl and jsonlite packages you're using). – Thomas Jul 10 '15 at 15:12

2 Answers2

1

Try package jsonlite maybe it will help you :

library("jsonlite")
your_json <- stream_in(url("https://api.stocktwits.com/api/2/streams/symbol/AAPL.json"))
str(your_json, max.level = 2)
View(your_json$messages)

If your json is in a .txt file you can do :

download.file(url = "https://api.stocktwits.com/api/2/streams/symbol/AAPL.json", destfile = "AAPL.txt")
your_json <- stream_in(file("AAPL.txt"))
Victorp
  • 13,636
  • 2
  • 51
  • 55
  • My .txt file is on my computer, instead of "APPL.txt" what do I have to write please? I try with the emplacement of the file on my computer : "C:\\....\\file.txt" but it does not work – Cec Jurczyk Jul 10 '15 at 15:50
  • Are you sure for the path ? What's the name of your file ? Try the code above. – Victorp Jul 10 '15 at 16:01
  • Yes I am sure of the path. It's "test1.txt" It's not data from an URL it's a file that I have on my computer from a test that I did before to get some tweets from an api. – Cec Jurczyk Jul 10 '15 at 16:41
  • json.text <- stream_in(file("C:/..../test1.txt")) opening file input connection. Found 5 lines... Error in parse_string(txt, bigint_as_char) : parse error: trailing garbage ","companyname":"3M Company"},{"datetime":"2015-07-07 09:10: (right here) ------^ In addition: Warning message: In readLines(con, n = pagesize, encoding = "UTF-8") : incomplete final line found on 'C:/.../test1.txt' closing file input connection. – Cec Jurczyk Jul 10 '15 at 17:14
  • Probably a malformation of the json itself. With `download.file(url = "https://api.stocktwits.com/api/2/streams/symbol/AAPL.json", destfile = "AAPL.txt")` I have no problem. – Victorp Jul 10 '15 at 18:27
  • Yes it is also working for me this way, but I don't want to import this file, I want to import the one on my computer, "test1"!! It's another one from a test I made weeks ago! It's not about appl, it's another one... {"datetime":"2015-07-08 09:10:00","subject":"MMM","sscore":"-0.2280","smean":"0.2593","svscore":"-0.2795","sdispersion":"0.375","svolume":"8","sbuzz":"0.6026","lastclose":"155.430000000","companyname":"3M Company"},{"datetime......... etc – Cec Jurczyk Jul 11 '15 at 12:50
0

try this

library(curl)
library(jsonlite)
AAPLapi <- 'https://api.stocktwits.com/api/2/streams/symbol/AAPL.json'
df=stream_in(curl(AAPLapi))
Ajay Ohri
  • 3,382
  • 3
  • 30
  • 60
  • df=stream_in(curl(AAPLapi)) opening curl input connection. Found 1 lines... binding pages together (no custom handler). closing curl input connection. Warning message: In readLines(con, n = pagesize, encoding = "UTF-8") : incomplete final line found on 'https://api.stocktwits.com/api/2/streams/symbol/AAPL.json' – Cec Jurczyk Jul 10 '15 at 15:09
  • see df to check the data – Ajay Ohri Jul 10 '15 at 15:12
  • 1
    Yes thank you, I found my data with View(df$messages) thank you very much – Cec Jurczyk Jul 10 '15 at 15:26