Skip to content

Instantly share code, notes, and snippets.

@davidicus
Created December 25, 2017 19:45
Show Gist options
  • Save davidicus/dbeae7be949c32237c47ae99c5e60556 to your computer and use it in GitHub Desktop.
Save davidicus/dbeae7be949c32237c47ae99c5e60556 to your computer and use it in GitHub Desktop.
Prettify JSON response for console.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
// Converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified, or optionally including only the specified properties if a replacer array is specified.
// @param {object} - The value to convert to a JSON string.
// @param {func/ array} - function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting/filtering the properties
// @return {json} - A JSON string representing the given value.
JSON.stringify(<object>, <function/array>, <integer>);
// const obj = {Name: "David", Age: 37, Sex: true}
// JSON.stringify(obj, null, 2);
// JSON.stringify(obj, ["Name"], 2);
// prints out
"{
"Name": "David",
"Age": 37,
"Sex": true
}"
"{
"Name": "David"
}"
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment