Removing a string using regular expression with stringr

664 views Asked by At

I am trying to get rid of the term union in the following json using str_replace_all function. I tried few regular expressions but they didn't work. Any suggestions on how to get rid of the full string of "union". Here is the code.

 library(jsonlite)

 json<- '{"open_issues":0,"union":{"avatar_url":"https://secure.gravatar.com/avatar/ed73cf7f24c091318e507d167c7b3456?d=https://a248.e.akamai.net/assets.mashups.com%2Fimages%2Fgravatars%2Fgravatar-orgs.png","gravatar_id":"ed73cf7f24c091318e507d167c7b3456","id":1556245,"login":"Toolpark","url":"https://api.mashups.com/users/Toolpark"},"owner":{"avatar_url":"https://secure.gravatar.com/avatar/ed73cf7f24c091318e507d167c7b3456?d=https://a248.e.akamai.net/assets.mashups.com%2Fimages%2Fgravatars%2Fgravatar-orgs.png","gravatar_id":"ed73cf7f24c091318e507d167c7b3456","id":1556245,"login":"Toolpark","url":"https://api.mashups.com/users/Toolpark"},"private":false,"pushed_at":"2012-07-12T11:43:31Z","size":964,"ssh_url":"[email protected]:Toolpark/irma.vit","svn_url":"https://mashups.com/Toolpark/irma","updated_at":"2012-07-12T11:43:31Z","url":"https://api.mashups.com/repos/Toolpark/irma","watchers":2}'

str_replace_all(json,"union[^}]*}\s*, "")
json <- fromJSON(json)
Error: '\s' is an unrecognized escape in character string starting ""union[^}]*}\s"

I am trying to get rid of this string exactly including the coma in the end:

"union":{"avatar_url":"https://secure.gravatar.com/avatar/ed73cf7f24c091318e507d167c7b3456?d=https://a248.e.akamai.net/assets.mashups.com%2Fimages%2Fgravatars%2Fgravatar-orgs.png","gravatar_id":"ed73cf7f24c091318e507d167c7b3456","id":1556245,"login":"Toolpark","url":"https://api.mashups.com/users/Toolpark"},

Any suggestions?

2

There are 2 answers

1
Shiva On BEST ANSWER

If I am guessing correct, you intend to delete the "union" element from the json line. So, why don't you change the line into json using fromJSON function and then try deleting the union element. The following code yields you a list without union

json1 <- ("your json line") 
my_list<- fromJSON(json1)   #convert into json format which gives you a list of elements
my_list["union"]<- NULL   #delete the union element
print(my_list)
1
karthik manchala On

You have been using your regex with bad syntax.. Use the following:

str_replace_all(json, "\"union[^}]*}\\s*,", "")

See DEMO