0 votes
1 view
in R Programming by (5.3k points)

I am trying to output a data frame in R as a json file to use for highcharts plots that I am making outside R. This is what my output looks like:-

[{"name":"alpha","value":1},{"name":"brave","value":2},{"name":"charlie","value":3}]

However, I want my output to look like this:-

[{name:"alpha",value:1},{name:"brave",value:2} {name:"charlie",value:3}] 

What should I do so that the names of my data frame (in this case name and value) are not put into quotes? If converting my data into a json file is not the best way, what else can/should I do?

library(tidyverse)

library(jsonlite)

data = tibble(name = c("alpha", "bravo", "charlie"), 

              value = c(1, 2, 3))

output = toJSON(data, dataframe="rows")

write(output, "output.txt")

1 Answer

0 votes
by (25.3k points)

To remove quotes from the output data frame, you can use the regex function from the base package as follows:

library("jsonlite")

library("stringr")

json_string <- toJSON(data, dataframe="rows")

temp <- str_replace_all(json_string, '"(\\w+)"\\s*:', '\\1:')

cat(temp)

write(temp, "output.txt")

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...