Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Write json scalars in R

In , 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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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\"}"    
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading