Extract data from list of dictionaries in R

Viewed 743

I have a dataframe with a column that contains a list of dictionaries as a character string.

Small reproducible example:

ids <- c('[{"type":"A","value":"666"},{"type":"M","value":"029"},{"type":"A","value":"ABC"},{"type":"E","value":"5"},{"type":"E34","value":"456"}]',
         '[{"type":"G","value":"334"},{"type":"A","value":"ZG5"},{"type":"E","value":"098"}]')

dt <- as.data.frame(ids)
dt

What I would like to do is to create a new column in dt called dt$A which contains the first value of "type":"A" for each corresponding row.

There is always at least one "type":"A" for each row.

In the example above I would like to get:

> dt$A
[1] "666" "ZG5"

So far I'have tried using rjson library but I wasn't able to extract the values that I want and I haven't figured out how to apply that to every row of dt.

Thanks

1 Answers
Related