In r, I have some data in a data frame and need to export it to jsonl. In jsonl each line is its own valid json. As the linked question shows, you can easily do that by applying jsonlite::toJSON() to each row. My problem is that I need one of the variables to be a scalar string, but toJSON makes any vector R vector into a list:
library(tidyverse)
library(jsonlite)
#>
#> Attaching package: 'jsonlite'
#> The following object is masked from 'package:purrr':
#>
#> flatten
d <- tibble(
id = 1:3,
text = c("this is a string", "this is another string", "yet another string")
)
jl <- d |>
transpose() |>
map_chr(toJSON)
jl
#> [1] "{\"id\":[1],\"text\":[\"this is a string\"]}"
#> [2] "{\"id\":[2],\"text\":[\"this is another string\"]}"
#> [3] "{\"id\":[3],\"text\":[\"yet another string\"]}"
I need text to be scalar. Desired output:
#> [1] "{\"id\":[1],\"text\":\"this is a string\"}"
#> [2] "{\"id\":[2],\"text\":\"this is another string\"}"
#> [3] "{\"id\":[3],\"text\":\"yet another string\"}"
I have tried to do this with regex, but I did not get it to work:
jl |>
str_replace('"text":\\[(.?)\\]', '"text":\\1')
#> [1] "{\"id\":[1],\"text\":[\"this is a string\"]}"
#> [2] "{\"id\":[2],\"text\":[\"this is another string\"]}"
#> [3] "{\"id\":[3],\"text\":[\"yet another string\"]}"
I would be happy with both a solution that writes scalars to begin with or one that fixes the regex to replace the square brackets.
>Solution :
We may use auto_unbox = TRUE
library(purrr)
library(jsonlite)
d |>
transpose() |>
map_chr(toJSON, auto_unbox = TRUE)
-output
[1] "{\"id\":1,\"text\":\"this is a string\"}"
[2] "{\"id\":2,\"text\":\"this is another string\"}"
[3] "{\"id\":3,\"text\":\"yet another string\"}"