0

I’m having in a function that iterates through several jsons some code like below:

my_tibble <- tibble(col_a = json$xyz$xyz,
col_b = json$xyz2$xyz2,
col_c = json$xyz3,
...)

I would like to have a NA returned if for instance col_b is missing in the json so here json$xyz2$xyz2.

Right now it just throw an error :

Error: Column "col_b" must be length 1 or 36, not 0

MattnDo
  • 444
  • 1
  • 5
  • 17

2 Answers2

1

One option could be to change the missing values to NA as part of json itself and then create your tibble. The below solution worked for my toy list.

library(tidyverse)
#First find max length in items of json
max_length <- max(sapply(json,length))

#Set length of each items so that `NA` is included 
json<- lapply(json, function(x){
  length(x)<-max_length
  x
  })

#Now cerate tibble
my_tibble <- tibble(col_a = json$xyz$xyz,
                    col_b = json$xyz2$xyz2,
                    col_c = json$xyz3)

OP can share details of json so that it can be verified against this solution.

MKR
  • 19,739
  • 4
  • 23
  • 33
  • I like your solution, however there’s a chance the jsons I’m iterating on don’t all have the same structure for elements I don’t use. I’d rather act on the tibble here. – MattnDo Apr 08 '18 at 04:18
  • @MattnDo Okay. A solution can be reached even in those cases. It will be better if you can share `sample` structure your `json` on which you want solution to work. – MKR Apr 08 '18 at 06:03
0

Ok I found a way to deal with my problem, which might not be the most elegant solution but at least is easy to implement:

NAfy <- function(x){
  if_else(is.null(x), NA, x)
}

my_tibble <- tibble(col_a = NAfy(json$xyz$xyz),
                    col_b = NAfy(json$xyz2$xyz2),
                    col_c = NAfy(json$xyz3))

Now what would be useful is to not copy-paste this function for every column, but this is for another time.

MattnDo
  • 444
  • 1
  • 5
  • 17