user1848018
user1848018

Reputation: 1106

Stripping special characters in R

I have the following string

> str    
[1]  "[ { \"category\" : \"book\"} , { \"category\" : \"Movie\"} , { \"category\" : \"Brand\"}]"

I want to strip it to get the following vector

> a
[1] "book" "Movie" "Brand" 

My problem is how to handle the "" and \ within the string to pass it to either grep or gsub in R. This is what I did and I get an error

> grep("^\[ \{ \\"category\\" : \\"([a-zA-Z0-9/]+)\\".*",str)
Error: '\[' is an unrecognized escape in character string starting "^\["

Am I even on the right track?

Upvotes: 1

Views: 244

Answers (1)

Spacedman
Spacedman

Reputation: 94182

Maybe the rjson package will sort you out:

> require(rjson)
> unlist(fromJSON(str))
  category category category 
    "book"  "Movie"  "Brand" 

ignoring the names ("category") that's what you want. Wrap in as.vector() to get rid of those.

Upvotes: 5

Related Questions