jsonlite icon indicating copy to clipboard operation
jsonlite copied to clipboard

parsing string "NA" is inconsistent when simplifyVector = TRUE

Open cjyetman opened this issue 4 years ago • 0 comments

When simplifyVector = TRUE and a vector contains only "NA" strings, they are converted to NA, otherwise they are converted to strings "NA". This can be a problem if the string "NA" is desired, as in https://github.com/christophergandrud/imfr/issues/21. This could possibly be avoided if feature request https://github.com/jeroen/jsonlite/issues/98 was implemented.

library(jsonlite)

fromJSON('{"iso2c":["NA","US"]}', simplifyVector = T)
#> $iso2c
#> [1] "NA" "US"

fromJSON('{"iso2c":["NA","NA"]}', simplifyVector = T)
#> $iso2c
#> [1] NA NA

fromJSON('{"iso2c":["NA","US"]}', simplifyVector = F)
#> $iso2c
#> $iso2c[[1]]
#> [1] "NA"
#> 
#> $iso2c[[2]]
#> [1] "US"

fromJSON('{"iso2c":["NA","NA"]}', simplifyVector = F)
#> $iso2c
#> $iso2c[[1]]
#> [1] "NA"
#> 
#> $iso2c[[2]]
#> [1] "NA"

In context...

library(jsonlite)

url <- 'http://dataservices.imf.org/REST/SDMX_JSON.svc/CompactData/DOT/.NA.TXG_FOB_USD?startPeriod=2018&endPeriod=2018'
head(fromJSON(url, simplifyVector = T)$CompactData$DataSet$Series$`@REF_AREA`)
#> [1] NA NA NA NA NA NA

fromJSON(url, simplifyVector = F)$CompactData$DataSet$Series[[1]]$`@REF_AREA`
#> [1] "NA"

url <- 'http://dataservices.imf.org/REST/SDMX_JSON.svc/CompactData/DOT/.US.TXG_FOB_USD?startPeriod=2018&endPeriod=2018'
head(fromJSON(url)$CompactData$DataSet$Series$`@REF_AREA`)
#> [1] "US" "US" "US" "US" "US" "US"

There is also strange inconsistency with jsonlite:::null_to_na...

library(jsonlite)

jsonlite:::null_to_na(list(iso2c = 'NA'))
#> $iso2c
#> [1] NA

jsonlite:::null_to_na(list(iso2c = 'US'))
#> $iso2c
#> [1] "US"

jsonlite:::null_to_na(list(iso2c = c('NA', 'US')))
#> $iso2c
#> [1] NA

jsonlite:::null_to_na(list(iso2c = c('US', 'NA')))
#> $iso2c
#> [1] "US" "NA"

jsonlite:::null_to_na(list(iso2c = c('US', 'US')))
#> $iso2c
#> [1] "US" "US"

cjyetman avatar Apr 30 '20 11:04 cjyetman