Skip to content

Instantly share code, notes, and snippets.

@shaunwallace
Created November 19, 2014 14:54
Show Gist options
  • Save shaunwallace/8a827599c9d41becbf80 to your computer and use it in GitHub Desktop.
Save shaunwallace/8a827599c9d41becbf80 to your computer and use it in GitHub Desktop.
JSON.stringify()
JSON.stringify({foo : true, bar : false}); // returns "{"foo":true,"bar":false}"
JSON.stringify([1, "false", false]); // returns "[1,"false",false]" note the last false is not surrounded by quotes
JSON.stringify({ a: 2 }, null, " "); // returns '{\n "a": 2\n}'
JSON.stringify({ a: 2 }, null, 4); // returns '{\n "a": 2\n}'
function replacer(k,v) {
if( typeof v === "string") return;
if( typeof v === "number") return 2*v;
return v;
}
JSON.stringify({ a: 2, b : "Some String" }, replacer, 4); // returns "{\n "a": 4}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment