Home > Back-end >  R : Argument 'txt' must be a JSON string, URL or file
R : Argument 'txt' must be a JSON string, URL or file

Time:08-17

I am working with the R programming language. I have a list object ("my_list") that looks something like this:

[[1]]
Response [https://abc]
  Date: 2022-08-17 02:33
  Status: 200
  Content-Type: application/json; charset=utf-8
  Size: 271 B
{
  "var1": "abc.123",
  "var2": "abc",
  "var3": "abc",
  "var4": "abc",
  "var5": "11.22, 33.44"


[[2]]
Response [https://abd]
  Date: 2022-08-17 02:33
  Status: 200
  Content-Type: application/json; charset=utf-8
  Size: 270 B
{
  "var1": "abd.123",
  "var2": "abd",
  "var3": "abd",
  "var4": "abd",
  "var5": "11.33,44.55"


[[3]]
Response [https://def]
  Date: 2022-08-17 02:33
  Status: 200
  Content-Type: application/json; charset=utf-8
  Size: 320 B
{
  "var1": "def.123",
  "var2": "def",
  "var3": "def",
  "var4": "def",
  "var5": "55.66, 77.88"
...

I would like to convert this into a data frame where the columns are "var1, var2, var3, var4, var5, var6 (split var 5)" and each row is the index of the list (e.g. [[1]], [[2]], [[3]], etc.). This would look something like this:

  index    var1 var2 var3 var4  var5   var6
1     1 abc.123  abc  abc  abc 11.22  33.44
2     2 abd.123  abd  abd  abd 11.33  44.55
3     3 def.123  def  def  def 55.66  77.88

Normally, I would have done something like this - but I get the following error:

data_frame = do.call(rbind.data.frame, my_list)

Error in (function (..., deparse.level = 1, make.row.names = TRUE, stringsAsFactors = FALSE,  : 
  invalid list argument: all variables should have the same length

I then thought that since this list appears to be in JSON format, maybe I should try the approach suggested in this link (How can I convert Json to data frame in R) - but I now get a new error:

fromJSON(my_list) %>% as.data.frame

Error: Argument 'txt' must be a JSON string, URL or file.

Can someone please show me what I am doing wrong and how can I fix this?

Thanks!

CodePudding user response:

fromJSON is not vectorizable, so you have to use lapply with it:

my_list <- list('{
  "var1": "abd.123",
  "var2": "abd",
  "var3": "abd",
  "var4": "abd",
  "var5": "11.33,44.55"}',
     '{
  "var1": "abd.123",
  "var2": "abd",
  "var3": "abd",
  "var4": "abd",
  "var5": "11.33,44.55"}',
     '{
  "var1": "abd.123",
  "var2": "abd",
  "var3": "abd",
  "var4": "abd",
  "var5": "11.33,44.55"}')

lapply(my_list, function(x) as.data.frame(fromJSON(x))) %>%
  dplyr::bind_rows()

#      var1 var2 var3 var4        var5
# 1 abd.123  abd  abd  abd 11.33,44.55
# 2 abd.123  abd  abd  abd 11.33,44.55
# 3 abd.123  abd  abd  abd 11.33,44.55
  • Related