I am trying to load a json file into a data.frame in R. But there is some list() which is null in my data.

Here is my json data:

json_file1 <- jsonlite::fromJSON('{"txtId":"20180101","data":{"user":[{"id":"123","phone":"00001","realName":"Eric","addr":{},"source":{},"registerDate":{},"type":0,"remain":{}}],"score":[]}}')
json_file2 <- jsonlite::fromJSON('{"txtId":"20180102","data":{"user":[{"id":"456","phone":"00002","realName":"Amy","addr":{},"source":{},"registerDate":{},"type":0,"remain":100}],"score":[]}}')
json_file = list(json_file1, json_file2)
zt.detail = lapply(json_file, function(y){
  if(!is.null(y$data$user)) data.frame(y$data$user, stringsAsFactors = F)
  })

when I rbind zt.detail, I get the error:

# > dat_callrecord = data.table::rbindlist(zt.detail, fill = T)
# Error in data.table::rbindlist(zt.detail, fill = T) : 
#   Column 4 of item 1 is length 0, inconsistent with first column of that item which is length 1. rbind/rbindlist doesn't recycle as it already expects each item to be a uniform list, data.frame or data.table
# > str(zt.detail[[1]])
# 'data.frame': 1 obs. of  9 variables:
#   $ id          : chr "123"
# $ phone       : chr "00001"
# $ realName    : chr "Eric"
# $ addr        :'data.frame':  1 obs. of  0 variables
# $ source      :'data.frame':  1 obs. of  0 variables
# $ registerDate:'data.frame':  1 obs. of  0 variables
# $ type        : int 0
# $ remain      :'data.frame':  1 obs. of  0 variables

The error was caused because the structure of my data contains data.frame of 1 observation but 0 variables. So I want to transfer those list() into NA before and get the following result:

> dat_callrecord
id phone realName type remain addr source registerDate
123 00001  Eric    0    NA     NA     NA    NA      
456 00002  Amy     0    100    NA     NA    NA

score:3

Accepted answer

We can loop through the list and if there is a data.frame, replace it with NA and then do the rbindlist

data.table::rbindlist(lapply(zt.detail, function(x) {
       x[] <- lapply(x, function(y) if(is.data.frame(y)) NA else y)
      x}))
#    id phone realName addr source registerDate type remain
#1: 123 00001     Eric   NA     NA           NA    0     NA
#2: 456 00002      Amy   NA     NA           NA    0    100