Skip to content

Instantly share code, notes, and snippets.

@kieranbarker
Last active February 24, 2021 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kieranbarker/16b709b4177dfa7b149ef6ab5315a089 to your computer and use it in GitHub Desktop.
Save kieranbarker/16b709b4177dfa7b149ef6ab5315a089 to your computer and use it in GitHub Desktop.
Format a JSON string for readability
/**
* Format a JSON string for readability
* @param {String} text The JSON string
* @param {Number|String} space The number of spaces, or a string, to indent by
* @returns {String} The newly formatted JSON string
* @license MIT
*/
function formatJSON (text, space = 2) {
return JSON.stringify(JSON.parse(text), null, space);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment