-2

Was handed off some code from a coworker to import/clean JSON files which apparently was working fine, but now I get the "$ operator is invalid for atomic vectors" error and I'm wondering what's gone wrong.. Any troubleshooting tips would be appreciated!

Since I can't open the JSON files in a useful way, it's tough to create input data, but here's the code I'm working with:

    library(jsonlite)
    library(stringr)
    library(tidyr)
    library(splitstackshape)

    eBfiles <- list.files(data_repository, full.names=T)

    eBors <- list()
    couldnt.find <- c()

    for(i in 1:length(eBfiles)){

      ecb <- fromJSON(eBfiles[i])
      ecb <- ecb$reportList

      for(j in 1:length(ecb)){                                        
        thr_id <- ecb[[j]]$identifier[1]  ## where the error is thrown

        thr_dt <- data.table(ecb[[j]]$rowList)

        thr_dt <- cSplit(thrm_dt, "V1", ",")

        thr_dt <- thr_dt %>%
          mutate(DateTime = ymd_hms(paste(V1_01, V1_02, sep = " "), 
                 tz = "PDT") - dhours(7),
                 V1_05 = as.character(V1_05)) %>%
          dplyr::select(DateTime, V1_05) %>%
          mutate(Hour = hour(DateTime)) %>%
          filter(as.character(Hour) %in% 
               c("14", "15", "16", "17", "18", "19", "20")) %>% 
          dplyr::select(DateTime, V1_05)

        setnames(thr_dt, "V1_05", "zoneMode")

        thr_rp <- (filter(ID_map, SNUM==thrm_d))$RP

        if (length(thr_rp) != 0) {
          if (length(thr_rp) != 1) {
            thr_rp <- sample(thr_rp,1)
          }
          thr_dt$RP <- rep(thr_rd)
          eBors <- list(eBors, thr_dt)
          eBors <- rbindlist(eBors)
        } else {
          couldnt.find <- c(couldnt.find, thr_d)
        }

      }
    }
gneissdata
  • 13
  • 5
  • There's a lot of code here that we can't run or test. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. At least identify exactly what line is throwing the error. – MrFlick Feb 05 '19 at 17:23
  • @MrFlick I added a comment where the error is being thrown - can't share the JSON files because they're proprietary and I can't open the JSON files to even see how to make example data because the code isn't working.. hands are a bit tied - hence why I asked for any troubleshooting tips... – gneissdata Feb 05 '19 at 17:27
  • Perhaps `ecb[[j]]` if not being read as a named list, but as a named vector? Try replacing the problem line with `thr_id <- ecb[[j]][["identifier"]][1]` and see if you get the error. – mikeck Feb 05 '19 at 17:40
  • A json file should be accessible via a plain text reader, e.g. notepad++. You can always look at the file directly to determine the structure. – mikeck Feb 05 '19 at 18:45

1 Answers1

-1

In case anyone else out there encounters this:

Don't use jsonlite - rather:
library(RJSONIO)

gneissdata
  • 13
  • 5
  • 1
    Actually, I prefer `jsonlite` for a variety of reasons. Also see [this document] (https://rstudio-pubs-static.s3.amazonaws.com/31702_9c22e3d1a0c44968a4a1f9656f1800ab.html) for some useful comparisons of output. – mikeck Feb 05 '19 at 18:07