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
  • 502,043
  • 27
  • 286
  • 360
  • 2
    Easy, use a JSON parser, and then iterate over your JSON content to generate the output you want. This is _not_ a job for `sub`, or any other R regex function. – Tim Biegeleisen May 15 '18 at 13:31
  • `s <- '{"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"}'; L <- readLines(textConnection(gsub("\\},", "}\n", s))); L <- L[L != ""]; ndjson::flatten(L)` – G. Grothendieck May 15 '18 at 14:46

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
  • 874,273
  • 37
  • 540
  • 662