Skip to content

Instantly share code, notes, and snippets.

@Uvacoder
Forked from DavidWells/stringify-no-quotes.js
Created January 2, 2022 01:51
Show Gist options
  • Select an option

  • Save Uvacoder/1e2e77586a00c8bcbcb1fd1ecadb29b1 to your computer and use it in GitHub Desktop.

Select an option

Save Uvacoder/1e2e77586a00c8bcbcb1fd1ecadb29b1 to your computer and use it in GitHub Desktop.
Stringify object with no quotes. https://jsfiddle.net/DerekL/mssybp3k/
function stringify(obj_from_json){
if(typeof obj_from_json !== "object" || Array.isArray(obj_from_json)){
// not an object, stringify using native function
return JSON.stringify(obj_from_json);
}
// Implements recursive object serialization according to JSON spec
// but without quotes around the keys.
let props = Object
.keys(obj_from_json)
.map(key => `${key}:${stringify(obj_from_json[key])}`)
.join(",");
return `{${props}}`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment