Converting from a list to a string and back


#1

Perhaps a trivial question but I can't get it to work: I get a huge as an API response that I completely flatten and convert to a string in order to upload it to DB. But how could I load it in R and convert that long string back to it's original list format?


#2

How do you save in a DB? JSON? I guess not, otherwise you won't have a question :slight_smile:
Can you show how string looks like in principle, without any real data in it?


#3

A JSON can't be stored in DB so you need to convert it to a string, like in the example below. I'm converting a list to a JSON here because that's eventually what the API returns. Then you're able to store the JSON-string in DB as a very long varchar.

example <- list(
  name = "konrad",
  surname = "semsch"
)

example <- jsonlite::toJSON(example)

example <- as.character(example)

In the end the example looks the following:

"{\"name\":[\"konrad\"],\"surname\":[\"semsch\"]}"

But here's the problem now - when I try to convert that back to a list so that I can work on it in R/ or simply process it in any way it eventually will only be this:

> as.list(example)
[[1]]
[1] "{\"name\":[\"konrad\"],\"surname\":[\"semsch\"]}"

Question: how can I parse that string so that it will have it's original list structure?


#4

Wouldn't it be just a simple fromJSON?

> jsonlite::fromJSON("{\"name\":[\"konrad\"],\"surname\":[\"semsch\"]}")
$name
[1] "konrad"

$surname
[1] "semsch"

I was thinking that you store data in a long string like that: "1,1,1,1,1". Converting that is a little bit more awkward, but since it's not the case, then I don't see many problems with the approach above.


#5

Unfortunately it's not that simple because I have a list structure on top:

jsonlite::fromJSON(t)
Error: lexical error: invalid char in json text.
list(message = "Stats calculate
(right here) ------^

So I have a list where the first object is a message and the second one is the JSON object that I need. I'm interested in retrieving the 2nd element of that list. So ideally I could convert that string directly to a list. Any ideas?


#6

Can you show the structure a bit more?
You can always use purrr::map and do anything you need there. So the function will be along the lines of:

convert_snd_element <- function(x){
  json <- x[[2]]
  jsonlite::fromJSON(json)
}

#7

So this example below represents exactly the structure I'm dealing with as it comes from the API

example <- list(
  list(message = "konrad semsch",
       data = list(
         x = 1,
         y = 2,
         z = 3
         )
       )
  )

which I then subsequently format to:

example <- as.character(example)
[1] "list(message = \"konrad semsch\", data = list(x = 1, y = 2, z = 3))"

When I try to use jsonlite::fromJSON it gives the same error as I have in my original set:

> jsonlite::fromJSON(example)
Error: lexical error: invalid char in json text.
                                       list(message = "konrad semsch",
                     (right here) ------^

So in this context your latest solution also would fail because the first this entire chunk needs to be parsed into a list format that R understands right?


#8

But your example is not JSON then.
Getting it back can be done via eval(parse(...)):

x <- "list(message = \"konrad semsch\", data = list(x = 1, y = 2, z = 3))"
res <- eval(parse(text = x))
str(res)
#> List of 2
#>  $ message: chr "konrad semsch"
#>  $ data   :List of 3
#>   ..$ x: num 1
#>   ..$ y: num 2
#>   ..$ z: num 3

Created on 2018-10-14 by the reprex package (v0.2.1)


#9

I knew it was easy... - thanks a lot got your help! :slight_smile: