extras/02_exportToJson.R

## Export JSON ##
library(StudyManagement)
#rdsFileName <- readRDS(file = rdsLoc)

outFolder <- paste0(basePath, '/studySpecificationAndResults/02_json')
for (i in (1:length(rdsFileName))) { # i = 3
  print(i)
  item <- paste0(names(rdsFileName)[[i]])
  if (item %in% c('conceptSet','characterization','incidenceRateAnalysis','estimation', 'cohort')) { #j = 2
    writeLines(paste0("working on ", item))
    for (j in (1:length(rdsFileName[[item]]))) {#j = 1
      if (item == 'conceptSet') {
        input <- rdsFileName[[item]][[j]]$conceptSetExpression$native
        if (!is.null(input)) {
        input <- jsonlite::toJSON(jsonlite::fromJSON(input), pretty = TRUE)
        }
      }
      if (item == 'characterization') {
        input <- rdsFileName[[item]][[j]]$specification$native
        if (!is.null(input)) {
          jsonFull <- jsonlite::fromJSON(input, flatten = TRUE)
          input <- jsonlite::toJSON(jsonFull, pretty = TRUE)
        }
        #does not seem to be fully specified. only appears to be labels
      }
      if (item == 'incidenceRateAnalysis') {
        input <- rdsFileName[[item]][[j]]$specification$native
        if (!is.null(input)) {
        jsonFull <- jsonlite::fromJSON(input)
        input <- jsonlite::toJSON(jsonFull, pretty = TRUE)
        }
        #incidence rate analysis import accepts nested json for EXPRESSION AND
        #the nested JSON may have escape characters
        #is this json working when imported into Atlas?
      }
      if (item == 'estimation') {
        input <- rdsFileName[[item]][[j]]$specification$native
        if (!is.null(input)) {
          jsonFull <- jsonlite::fromJSON(input, flatten = TRUE)
          #jsonFull <- jsonlite::fromJSON(input)
          #jsonSpecification <- jsonlite::toJSON(jsonlite::fromJSON(jsonFull$specification), pretty = TRUE)
          #jsonFull$specification <- jsonSpecification
          input <- jsonlite::toJSON(jsonFull, pretty = TRUE)
          #needs additional step to parse nested json - specification. not working
        }
      }
      if (item == 'cohort') {
        input <- rdsFileName[[item]][[j]]$specification$native
        if (!is.null(input)) {
          jsonFull <- jsonlite::fromJSON(input)
          jsonCohortExpression <- jsonlite::fromJSON(jsonFull$expression)
          input <- jsonlite::toJSON(jsonCohortExpression, pretty = TRUE)
        }
      }

      if (!is.null(input)) {
        outputFileName <- paste0(names(rdsFileName[[item]])[[j]], '.json')
        outputPathWithFile <- paste0(basePath, '/studySpecificationAndResults/02_json/', item, '/', outputFileName)

        write(x = input, file = outputPathWithFile)
      }
    }
  }
  print(paste0("Done. Check location: ",  file.path(paste0(basePath, '/studySpecificationAndResults/02_json/'))))
}
gowthamrao/StudyManagement documentation built on March 9, 2020, 10:48 p.m.