Created
February 20, 2024 10:26
-
-
Save geraintluff/6a49aad2b63f31ccc973a001f7d21672 to your computer and use it in GitHub Desktop.
Simple RegEx for slightly prettier JSON-ish output (unquoted keys, single-item objects/arrays on one line)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JSON.simplified = v => { | |
return JSON.stringify(v, null, '\t') | |
.replace(/(\n\t*)"([a-z][a-z0-9_]*)"/ig, '$1$2') | |
.replace(/(\n[\sa-z0-9_:]+)\{\n\t*(.*)\n\t+\}/ig, '$1{$2}') | |
.replace(/(\n[\sa-z0-9_:]+)\[\n\t*(.*)\n\t+\]/ig, '$1[$2]'); | |
}; | |
JSON.parseSimplified = t => { | |
return JSON.parse(t | |
.replace(/(\n[\sa-z0-9_:]+)([\[\{])([^\n])/ig, '$1$2\n$3') | |
.replace(/(\n\t*)([a-z][a-z0-9_]*):/ig, '$1"$2":') | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment