Skip to content

Instantly share code, notes, and snippets.

@ozzi-
Created November 28, 2019 08:52
Show Gist options
  • Select an option

  • Save ozzi-/5955f058f2d0ce0c70ccd88b83a1c751 to your computer and use it in GitHub Desktop.

Select an option

Save ozzi-/5955f058f2d0ce0c70ccd88b83a1c751 to your computer and use it in GitHub Desktop.
recursive iterate through json values and sanitize strings with escapeHtml
function sanitizeJSONValues(obj){
for (var k in obj){
if (typeof obj[k] == "object" && obj[k] !== null){
sanitizeJSON(obj[k]);
}
else{
if(typeof obj[k]=="string"){
obj[k] = escapeHtml(obj[k]);
}
}
}
}
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment