Skip to content

Instantly share code, notes, and snippets.

@CostasAK
Created March 26, 2021 09:55
Show Gist options
  • Select an option

  • Save CostasAK/ec8d3a4b562e03dd578ff46b0713fba4 to your computer and use it in GitHub Desktop.

Select an option

Save CostasAK/ec8d3a4b562e03dd578ff46b0713fba4 to your computer and use it in GitHub Desktop.
Iterate through a nested object and replace reserved JSON characters.
const iterate = (obj) => {
Object.keys(obj).forEach(key => {
if (typeof obj[key] === 'object') {
iterate(obj[key])
} else if (typeof obj[key] === 'string' || obj[key] instanceof String) {
obj[key] = obj[key].replace(/\n/g,'\\u000a').replace(/"/g,'\\u0022').replace(/'/g,'\\u0027');
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment