0

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":"vit@mashups.com: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?

user3570187
  • 1,743
  • 3
  • 17
  • 34

2 Answers2

2

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)
Shiva
  • 789
  • 6
  • 15
  • i have a huge json file which is difficult to be copied into the .R file and i am seeking some alternative solution. Note that I don't have a valid json file and i need to make it to valid json and do the operations! Thanks so much! – user3570187 Jun 23 '15 at 00:08
1

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

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

See DEMO

karthik manchala
  • 13,492
  • 1
  • 31
  • 55
  • 1
    Thank you so much! I am getting this error: Error in stri_replace_all_regex(string, pattern, replacement, vectorize_all = vec, : Syntax error in regexp pattern. (U_REGEX_RULE_SYNTAX). Not sure if it is a r package issue. – user3570187 Jun 23 '15 at 13:48