Remove trailing and leading quotes from character vector

Hi Community,

I would like to remove trailing and leading quotes from a character vector and later on convert to json object.

In this case when I use noquote() function with toJSON() from jsonlite package, an error is returned as Error: No method asJSON S3 class: noquote

Example code is below

#> character vector
reproducibile_users_invite <-c("{\"firstName\":\"Emmanuel\",\"surname\":\"Emmanuel2\",\"email\":\"Emmanuel@email.com\",\"userCredentials\":{\"username\":\"EEmmanuel2\",\"userRoles\":[{\"id\":\"cBCK2qrYxyv\"}],\"catDimensionConstraints\":[{\"id\":\"MnQamDMlY87\"}]},\"organisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"dataViewOrganisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"userGroups\":[{\"id\":\"MA3AsjoPf3o\"}]}", 
"{\"firstName\":\"Grant\",\"surname\":\"Grant2\",\"email\":\"Grant@email.com\",\"userCredentials\":{\"username\":\"GGrant2\",\"userRoles\":[{\"id\":\"cBCK2qrYxyv\"}],\"catDimensionConstraints\":[{\"id\":\"MnQamDMlY87\"}]},\"organisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"dataViewOrganisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"userGroups\":[{\"id\":\"MA3AsjoPf3o\"}]}", 
"{\"firstName\":\"Patience\",\"surname\":\"Patience2\",\"email\":\"Patience@email.com\",\"userCredentials\":{\"username\":\"PPatience2\",\"userRoles\":[{\"id\":\"cBCK2qrYxyv\"}],\"catDimensionConstraints\":[{\"id\":\"MnQamDMlY87\"}]},\"organisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"dataViewOrganisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"userGroups\":[{\"id\":\"MA3AsjoPf3o\"}]}", 
"{\"firstName\":\"Thomas\",\"surname\":\"Thomas2\",\"email\":\"Thomas@email.com\",\"userCredentials\":{\"username\":\"TThomas2\",\"userRoles\":[{\"id\":\"cBCK2qrYxyv\"}],\"catDimensionConstraints\":[{\"id\":\"MnQamDMlY87\"}]},\"organisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"dataViewOrganisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"userGroups\":[{\"id\":\"MA3AsjoPf3o\"}]}", 
"{\"firstName\":\"Andrew\",\"surname\":\"Andrew2\",\"email\":\"Andrew@email.com\",\"userCredentials\":{\"username\":\"AAndrew2\",\"userRoles\":[{\"id\":\"cBCK2qrYxyv\"}],\"catDimensionConstraints\":[{\"id\":\"MnQamDMlY87\"}]},\"organisationUnits\":[{\"id\":\"nDd4i6SPeth\"}],\"dataViewOrganisationUnits\":[{\"id\":\"nDd4i6SPeth\"}],\"userGroups\":[{\"id\":\"MA3AsjoPf3o\"}]}", 
"{\"firstName\":\"Nicholas\",\"surname\":\"Nicholas2\",\"email\":\"Nicholas@email.com\",\"userCredentials\":{\"username\":\"NNicholas2\",\"userRoles\":[{\"id\":\"cBCK2qrYxyv\"}],\"catDimensionConstraints\":[{\"id\":\"MnQamDMlY87\"}]},\"organisationUnits\":[{\"id\":\"nDd4i6SPeth\"}],\"dataViewOrganisationUnits\":[{\"id\":\"nDd4i6SPeth\"}],\"userGroups\":[{\"id\":\"MA3AsjoPf3o\"}]}"
)

jsonlite::toJSON(list(users=noquote(reproducibile_users_invite)),auto_unbox=TRUE)

#> Error: No method asJSON S3 class: noquote
1 Like

You can remove quotes with stringr::str_remove_all()

library(stringr)

reproducibile_users_invite <-c("{\"firstName\":\"Emmanuel\",\"surname\":\"Emmanuel2\",\"email\":\"Emmanuel@email.com\",\"userCredentials\":{\"username\":\"EEmmanuel2\",\"userRoles\":[{\"id\":\"cBCK2qrYxyv\"}],\"catDimensionConstraints\":[{\"id\":\"MnQamDMlY87\"}]},\"organisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"dataViewOrganisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"userGroups\":[{\"id\":\"MA3AsjoPf3o\"}]}", 
                               "{\"firstName\":\"Grant\",\"surname\":\"Grant2\",\"email\":\"Grant@email.com\",\"userCredentials\":{\"username\":\"GGrant2\",\"userRoles\":[{\"id\":\"cBCK2qrYxyv\"}],\"catDimensionConstraints\":[{\"id\":\"MnQamDMlY87\"}]},\"organisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"dataViewOrganisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"userGroups\":[{\"id\":\"MA3AsjoPf3o\"}]}", 
                               "{\"firstName\":\"Patience\",\"surname\":\"Patience2\",\"email\":\"Patience@email.com\",\"userCredentials\":{\"username\":\"PPatience2\",\"userRoles\":[{\"id\":\"cBCK2qrYxyv\"}],\"catDimensionConstraints\":[{\"id\":\"MnQamDMlY87\"}]},\"organisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"dataViewOrganisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"userGroups\":[{\"id\":\"MA3AsjoPf3o\"}]}", 
                               "{\"firstName\":\"Thomas\",\"surname\":\"Thomas2\",\"email\":\"Thomas@email.com\",\"userCredentials\":{\"username\":\"TThomas2\",\"userRoles\":[{\"id\":\"cBCK2qrYxyv\"}],\"catDimensionConstraints\":[{\"id\":\"MnQamDMlY87\"}]},\"organisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"dataViewOrganisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"userGroups\":[{\"id\":\"MA3AsjoPf3o\"}]}", 
                               "{\"firstName\":\"Andrew\",\"surname\":\"Andrew2\",\"email\":\"Andrew@email.com\",\"userCredentials\":{\"username\":\"AAndrew2\",\"userRoles\":[{\"id\":\"cBCK2qrYxyv\"}],\"catDimensionConstraints\":[{\"id\":\"MnQamDMlY87\"}]},\"organisationUnits\":[{\"id\":\"nDd4i6SPeth\"}],\"dataViewOrganisationUnits\":[{\"id\":\"nDd4i6SPeth\"}],\"userGroups\":[{\"id\":\"MA3AsjoPf3o\"}]}", 
                               "{\"firstName\":\"Nicholas\",\"surname\":\"Nicholas2\",\"email\":\"Nicholas@email.com\",\"userCredentials\":{\"username\":\"NNicholas2\",\"userRoles\":[{\"id\":\"cBCK2qrYxyv\"}],\"catDimensionConstraints\":[{\"id\":\"MnQamDMlY87\"}]},\"organisationUnits\":[{\"id\":\"nDd4i6SPeth\"}],\"dataViewOrganisationUnits\":[{\"id\":\"nDd4i6SPeth\"}],\"userGroups\":[{\"id\":\"MA3AsjoPf3o\"}]}"
)

str_remove_all(reproducibile_users_invite, '"')
#> [1] "{firstName:Emmanuel,surname:Emmanuel2,email:Emmanuel@email.com,userCredentials:{username:EEmmanuel2,userRoles:[{id:cBCK2qrYxyv}],catDimensionConstraints:[{id:MnQamDMlY87}]},organisationUnits:[{id:LndFoMpqBfp}],dataViewOrganisationUnits:[{id:LndFoMpqBfp}],userGroups:[{id:MA3AsjoPf3o}]}"
#> [2] "{firstName:Grant,surname:Grant2,email:Grant@email.com,userCredentials:{username:GGrant2,userRoles:[{id:cBCK2qrYxyv}],catDimensionConstraints:[{id:MnQamDMlY87}]},organisationUnits:[{id:LndFoMpqBfp}],dataViewOrganisationUnits:[{id:LndFoMpqBfp}],userGroups:[{id:MA3AsjoPf3o}]}"            
#> [3] "{firstName:Patience,surname:Patience2,email:Patience@email.com,userCredentials:{username:PPatience2,userRoles:[{id:cBCK2qrYxyv}],catDimensionConstraints:[{id:MnQamDMlY87}]},organisationUnits:[{id:LndFoMpqBfp}],dataViewOrganisationUnits:[{id:LndFoMpqBfp}],userGroups:[{id:MA3AsjoPf3o}]}"
#> [4] "{firstName:Thomas,surname:Thomas2,email:Thomas@email.com,userCredentials:{username:TThomas2,userRoles:[{id:cBCK2qrYxyv}],catDimensionConstraints:[{id:MnQamDMlY87}]},organisationUnits:[{id:LndFoMpqBfp}],dataViewOrganisationUnits:[{id:LndFoMpqBfp}],userGroups:[{id:MA3AsjoPf3o}]}"        
#> [5] "{firstName:Andrew,surname:Andrew2,email:Andrew@email.com,userCredentials:{username:AAndrew2,userRoles:[{id:cBCK2qrYxyv}],catDimensionConstraints:[{id:MnQamDMlY87}]},organisationUnits:[{id:nDd4i6SPeth}],dataViewOrganisationUnits:[{id:nDd4i6SPeth}],userGroups:[{id:MA3AsjoPf3o}]}"        
#> [6] "{firstName:Nicholas,surname:Nicholas2,email:Nicholas@email.com,userCredentials:{username:NNicholas2,userRoles:[{id:cBCK2qrYxyv}],catDimensionConstraints:[{id:MnQamDMlY87}]},organisationUnits:[{id:nDd4i6SPeth}],dataViewOrganisationUnits:[{id:nDd4i6SPeth}],userGroups:[{id:MA3AsjoPf3o}]}"
1 Like

@andresrcs,

Your solution removes all the quotes, and that's not my requirement.

My requirement is as below. the quotes remain but not the leading and training ones.

#> [1] {\"firstName\":\"Emmanuel\",\"surname\":\"Emmanuel2\",\"email\":\"Emmanuel@email.com\",\"userCredentials\":{\"username\":\"EEmmanuel2\",\"userRoles\":[{\"id\":\"cBCK2qrYxyv\"}],\"catDimensionConstraints\":[{\"id\":\"MnQamDMlY87\"}]},\"organisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"dataViewOrganisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"userGroups\":[{\"id\":\"MA3AsjoPf3o\"}]}
#> [2] {\"firstName\":\"Grant\",\"surname\":\"Grant2\",\"email\":\"Grant@email.com\",\"userCredentials\":{\"username\":\"GGrant2\",\"userRoles\":[{\"id\":\"cBCK2qrYxyv\"}],\"catDimensionConstraints\":[{\"id\":\"MnQamDMlY87\"}]},\"organisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"dataViewOrganisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"userGroups\":[{\"id\":\"MA3AsjoPf3o\"}]}            
#> [3] {\"firstName\":\"Patience\",\"surname\":\"Patience2\",\"email\":\"Patience@email.com\",\"userCredentials\":{\"username\":\"PPatience2\",\"userRoles\":[{\"id\":\"cBCK2qrYxyv\"}],\"catDimensionConstraints\":[{\"id\":\"MnQamDMlY87\"}]},\"organisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"dataViewOrganisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"userGroups\":[{\"id\":\"MA3AsjoPf3o\"}]}
#> [4] {\"firstName\":\"Thomas\",\"surname\":\"Thomas2\",\"email\":\"Thomas@email.com\",\"userCredentials\":{\"username\":\"TThomas2\",\"userRoles\":[{\"id\":\"cBCK2qrYxyv\"}],\"catDimensionConstraints\":[{\"id\":\"MnQamDMlY87\"}]},\"organisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"dataViewOrganisationUnits\":[{\"id\":\"LndFoMpqBfp\"}],\"userGroups\":[{\"id\":\"MA3AsjoPf3o\"}]}        
#> [5] {\"firstName\":\"Andrew\",\"surname\":\"Andrew2\",\"email\":\"Andrew@email.com\",\"userCredentials\":{\"username\":\"AAndrew2\",\"userRoles\":[{\"id\":\"cBCK2qrYxyv\"}],\"catDimensionConstraints\":[{\"id\":\"MnQamDMlY87\"}]},\"organisationUnits\":[{\"id\":\"nDd4i6SPeth\"}],\"dataViewOrganisationUnits\":[{\"id\":\"nDd4i6SPeth\"}],\"userGroups\":[{\"id\":\"MA3AsjoPf3o\"}]}        
#> [6] {\"firstName\":\"Nicholas\",\"surname\":\"Nicholas2\",\"email\":\"Nicholas@email.com\",\"userCredentials\":{\"username\":\"NNicholas2\",\"userRoles\":[{\"id\":\"cBCK2qrYxyv\"}],\"catDimensionConstraints\":[{\"id\":\"MnQamDMlY87\"}]},\"organisationUnits\":[{\"id\":\"nDd4i6SPeth\"}],\"dataViewOrganisationUnits\":[{\"id\":\"nDd4i6SPeth\"}],\"userGroups\":[{\"id\":\"MA3AsjoPf3o\"}]}

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.