Created
November 28, 2019 08:52
-
-
Save ozzi-/5955f058f2d0ce0c70ccd88b83a1c751 to your computer and use it in GitHub Desktop.
recursive iterate through json values and sanitize strings with escapeHtml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, "<") | |
| .replace(/>/g, ">") | |
| .replace(/"/g, """) | |
| .replace(/'/g, "'"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment