Skip to content

Instantly share code, notes, and snippets.

@geraintluff
Created February 20, 2024 10:26
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 geraintluff/6a49aad2b63f31ccc973a001f7d21672 to your computer and use it in GitHub Desktop.
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)
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