Skip to content

Instantly share code, notes, and snippets.

@mpettis
Created September 6, 2018 21:41
Show Gist options
  • Save mpettis/a780bed132a5b13d2ceceb30546d1700 to your computer and use it in GitHub Desktop.
Save mpettis/a780bed132a5b13d2ceceb30546d1700 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(jsonlite)
#>
#> Attaching package: 'jsonlite'
#> The following object is masked from 'package:purrr':
#>
#> flatten
# Ref: https://stackoverflow.com/questions/7944809/assigning-null-to-a-list-element-in-r
# JSON with 'null' keyword gets a null when cast to list object.
json_with_null <- '{"a": 1, "b": "B", "c": null}'
json_with_null %>% fromJSON()
#> $a
#> [1] 1
#>
#> $b
#> [1] "B"
#>
#> $c
#> NULL
# This works as well
lstt <- list(a=1, b="B", c=NULL)
str(lstt)
#> List of 3
#> $ a: num 1
#> $ b: chr "B"
#> $ c: NULL
# ! Does not work, removes whole entry
lstt[["c"]] <- NULL
str(lstt)
#> List of 2
#> $ a: num 1
#> $ b: chr "B"
# This however does NOT QUITE work
lstt[["c"]] <- list(NULL)
str(lstt)
#> List of 3
#> $ a: num 1
#> $ b: chr "B"
#> $ c:List of 1
#> ..$ : NULL
# This does...
lstt["c"] <- list(NULL)
str(lstt)
#> List of 3
#> $ a: num 1
#> $ b: chr "B"
#> $ c: NULL
# Created on 2018-09-06 by the [reprex package](http://reprex.tidyverse.org) (v0.2.0).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment