I'm trying to use R to create some API requests and using the jsonlite package to do so. I have an element in my JSON request named selected that needs to be set to "::total::": "". Everything I have tried results in the addition of a "\". I usually get something like this ... "::total::\": \"". Any ideas on how to solve this problem?
Here is my code:
reportsuite.id = "myRSID"
date.from = 2017-06-23
date.to = 2017-08-23
metrics = c("pageviews", "visits", "uniquevisitors")
elements = myVar
top = 0
start = 0
selected = '::total::": "'
search = c()
search.type = "or"
date.granularity = "day"
segment.id = ""
segment.inline = ""
classification = character(0)
anomaly.detection = FALSE
data.current = TRUE
expedite = FALSE
interval.seconds = 5
max.attempts = 120
validate = TRUE
enqueueOnly = FALSE
report.description <- c()
report.description$reportDescription <- c(data.frame(matrix(ncol = 0,
nrow = 1)))
report.description$reportDescription$dateFrom <- unbox(date.from)
report.description$reportDescription$dateTo <- unbox(date.to)
report.description$reportDescription$reportSuiteID <- unbox(reportsuite.id)
report.description$reportDescription$dateGranularity <- unbox(date.granularity)
report.description$reportDescription$elementDataEncoding <- unbox("utf8")
if (segment.inline != "") {
report.description$reportDescription$segments <- list(segment.inline)
}
if (as.list(segment.id)[1] == "") {
report.description$reportDescription$segment_id <- unbox(segment.id)
} else {
report.description$reportDescription$segments <- data.frame(id = segment.id)
}
if (anomaly.detection == TRUE) {
report.description$reportDescription$anomalyDetection <- unbox(anomaly.detection)
}
if (data.current == TRUE) {
report.description$reportDescription$currentData <- unbox(data.current)
}
if (expedite == TRUE) {
report.description$reportDescription$expedite <- unbox(expedite)
}
report.description$reportDescription$metrics = data.frame(id = metrics)
elements.formatted <- list()
i <- 0
for (element in elements) {
i <- i + 1
if (i == 1) {
working.element <- list(id = unbox(element), top = unbox(top[1]),
startingWith = unbox(start))
if (length(selected) != 0) {
working.element[["selected"]] <- selected
}
if (length(search) != 0) {
working.element[["search"]] <- list(type = unbox(search.type),
keywords = search)
}
} else {
if (length(top) >= i) {
working.element <- list(id = unbox(element),
top = unbox(top[i]))
} else {
working.element <- list(id = unbox(element),
top = unbox("50000"))
}
}
if (length(classification) >= i) {
working.element[["classification"]] <- unbox(classification[i])
}
if (length(elements.formatted) > 0) {
elements.formatted <- append(elements.formatted,
list(working.element))
} else {
elements.formatted <- list(working.element)
}
}
report.description$reportDescription$elements <- elements.formatted
Results in:
toJSON(report.description)
{"reportDescription":{"dateFrom":"2017-06-23","dateTo":"2017-08-23","reportSuiteID":"myRSID","dateGranularity":"day","elementDataEncoding":"utf8","segment_id":"","currentData":true,"metrics":[{"id":"pageviews"},{"id":"visits"},{"id":"uniquevisitors"}],"elements":[{"id":"evar32","top":0,"startingWith":0,"selected":["::total::\": \""]}]}}
Thanks! Jess