Toolbox
Toolbox

Reputation: 2491

R - Create string with escaped quotes, with input from variables

I need to create this string, with exact content, in order to be used in an API REST client.

body =
      "{
        \"epic\":      \"sweden\",
        \"direction\": \"BUY\"
}"

I am already using the solution by creating the string without calling variables. I am now looking to build the string with input from variables.

I need a solution that does not add more R packages. Preferably with minimum complexity for a good overview. I hope to avoid long regex patterns (but if regex is a good recommended way-forward, I am willing to consider it).

In attemp-1 and attempt-2, I have intentionally left out the curly brackets, to keep the question and its code minimized. They curly brackets needs to be part of solution, though.

Attempts so far:

Attemp-1 (solve with paste):

epic1        <- paste0("\"", "sweden")
direction1   <- paste0("BUY", "\"")
create.body1 <- c(epic1, "," ,direction1)

Result:

"\"sweden" ","        "BUY\"" 

Problem: Every provided variable input has quotation around. Also, the escape characers are only added as a wrapper around the complete string, not as needed, per each key and value.

Attemp-2 (strip out quotes with [noQuote]):

epic2        <- paste0("\"", "sweden")
direction2   <- paste0("BUY", "\"")
create.body2 <- noquote(c(epic2, "," ,direction2))

Result:

"sweden ,       BUY"   

Problem: The needed escape characters , backslash, are gone.

Attemp-3 (pre-construct [key/value pairs] prior to build body string):

# Curly brackets.
curly.bracket.left <- "{"
curly.bracket.right <- "}"

# Epic build [key/pair]
epic_key   <- "\"epic\": "
epic_value <- "\"sweden\""
epic_pair  <- c(epic_key, epic_value)

# Direction build [key/pair]
direction_key   <- "\"direction\": "
direction_value <- "\"BUY\""
direction_pair  <- c(direction_key, direction_value)

# Construct body string.
build.body <- c(
                curly.bracket.left,
                epic_pair,
                direction_pair,
                curly.bracket.right
)

Result:

"{" "\"epic\": " "\"sweden\"" "\"direction\": " "\"BUY\"" "}" 

Problem: There are to many quotation within the body string. All [\"] are fine though.

Upvotes: 0

Views: 504

Answers (1)

Humpelstielzchen
Humpelstielzchen

Reputation: 6441

I started from your 3rd attempt. The result looks like what you're after. I just switched c() for paste() basically, and inserted some space where there is space in your template. Is that what you need?

curly.bracket.left <- "{"
curly.bracket.right <- "}"

# Epic build [key/pair]
epic_key   <- "\"epic\": "
epic_value <- "  \"sweden\","
epic_pair  <- c(epic_key, epic_value)

# Direction build [key/pair]
direction_key   <- "\"direction\": "
direction_value <- "\"BUY\""
direction_pair  <- c(direction_key, direction_value)

# Construct body string.
build.body <- c(
  curly.bracket.left,
  epic_pair,
  direction_pair,
  curly.bracket.right
)

string <- paste(curly.bracket.left, epic_key, epic_value, direction_key, direction_value, curly.bracket.right)

> print(string)
[1] "{ \"epic\":    \"sweden\", \"direction\":  \"BUY\" }" 

Upvotes: 1

Related Questions