Skip to content

Instantly share code, notes, and snippets.

@daanta-real
Last active November 17, 2022 05:36
Show Gist options
  • Select an option

  • Save daanta-real/57bab373de494b221b81e90fcd193046 to your computer and use it in GitHub Desktop.

Select an option

Save daanta-real/57bab373de494b221b81e90fcd193046 to your computer and use it in GitHub Desktop.
Beautify JSON
// if you put isHTML parameter true you'll get newline char as br HTML tag, or just get "\n"
function jsonBeautifier(jsonTxt, isHTML) {
let result = JSON.stringify(JSON.parse(jsonTxt), null, 4);
if(isHTML) result = result.replaceAll("\n", "<br />");
return result;
}
/* Sample */
const jsonTxt = `{
"a": {
"userId": 1,
"firstName": "AAAAA",
"lastName": "as23",
"phoneNumber": "123456",
"emailAddress": "AAAAA@test.com",
"homepage": "https://11.tistory.com/1"
},
"b": {
"userId": 2,
"firstName": "BBBB",
"lastName": "h5jdd",
"phoneNumber": "123456",
"homepage": "https://11.tistory.com/2"
}
}`;
console.log(jsonBeautifier(jsonTxt));
console.log(jsonBeautifier(jsonTxt, true));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment