Skip to content

Instantly share code, notes, and snippets.

@qb20nh
Last active December 17, 2021 06:35
Show Gist options
  • Save qb20nh/583f73ca466017a4f7d5c0284ba0449b to your computer and use it in GitHub Desktop.
Save qb20nh/583f73ca466017a4f7d5c0284ba0449b to your computer and use it in GitHub Desktop.
Simple json formatter
const json_format = (jsons, level = 2) => {
let i = 0
return JSON.stringify(JSON.parse(jsons)).replace(/\[\]|\{\}|[{[,\]}]/g,
c => ['[]', '{}'].includes(c) ? c : '{[,'.includes(c) ? c + '\n' + (','.includes(c) ? ' '.repeat(i * level) : ' '.repeat(++i * level)) : '\n' + ' '.repeat(--i * level) + c)
.replace(/"(?:[^"]|\\")+"(:)/g, '$& ')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment