Reputation: 157
This may seem like a simple problem to solve, but it's beyond me. I have the following enclosed string
'{"foo":"bar","x":"<SOME VAR>"}'
I want to pass a variable into the place of <SOME VAR>
. What would be the best way to achieve this? Any help is greatly appreciated.
Upvotes: 1
Views: 135
Reputation: 937
(re-posting this as an answer as requested - glad to know it worked!)
can't you use gsub
?
string_name <- gsub(pattern = "<SOME VAR>", replacement = variable, x = string_name)
where string_name is the name of your example string
Upvotes: 2
Reputation: 3882
Your string is, in fact, a JSON object and thus you can use any JSON parser in R to convert your data to a data.frame
, easy to manipulate.
library(jsonlite)
x <- '{"foo":"bar","x":"<SOME VAR>"}'
df <- fromJSON(x)
my_value <- "This is the value I want"
df$x <- my_value
df
#$foo
#[1] "bar"
#
#$x
#[1] "This is the value I want"
You can convert the data.frame
again to JSON with:
toJSON(df, auto_unbox = TRUE)
Upvotes: 1
Reputation: 341
Can you store it as a list like:
a <- list("foo" = "bar", "x" = "<SOME VAR>")
If yes you could just fill it using the $
operator:
a$x <- 3
Upvotes: 1