1

Say I have a string:

{"id":"35","value":"0.2"},{"id":"1462","value":"0.2"},
{"id":"1109","value":"0.2"},{"id":"220","value":"0.2"},
{"id":"211","value":"0.1"}

I need to extract substrings in each {}

Than create columns with an id in the header and a number corresponding to the id like:

35  1462 1109 220 211
----------
0.2  0.2 0.2  0.2 0.1
Tim Biegeleisen
  • 451,927
  • 24
  • 239
  • 318

1 Answers1

1

We can use jsonlite after pasteing [, ] at the start and end respectively

d1 <- jsonlite::fromJSON(paste0('[', str1, ']'))

It will be a 2 column dataset which can be converted to 4 column by

setNames(as.data.frame.list(d1$value), d1$id)
#   35 1462 1109 220 211
#1 0.2  0.2  0.2 0.2 0.1

Suppose, we have multiple strings, then collapse those strings to a single one and apply the fromJSON

str2 <- c(str1, str1)
d1 <- jsonlite::fromJSON(paste0("[", paste(str2, collapse=",\n"), "]"))

data

str1 <- '{"id":"35","value":"0.2"},{"id":"1462","value":"0.2"},{"id":"1109","value":"0.2"},{"id":"220","value":"0.2"},{"id":"211","value":"0.1"}'
akrun
  • 789,025
  • 32
  • 460
  • 575