Skip to content

Instantly share code, notes, and snippets.

@zlataovce
Last active July 2, 2026 13:29
Show Gist options
  • Select an option

  • Save zlataovce/18bd7001bcacdfce05b900b9873ec024 to your computer and use it in GitHub Desktop.

Select an option

Save zlataovce/18bd7001bcacdfce05b900b9873ec024 to your computer and use it in GitHub Desktop.
A slicer script for formatting JSON.
const indentSizeOption = {
type: "radio",
id: "json-formatter/indent-size",
label: "Indent size",
items: [
{
id: "2",
label: "2 spaces",
},
{
id: "4",
label: "4 spaces",
},
{
id: "8",
label: "8 spaces",
},
],
selected: "4",
};
const utf8Decoder = new TextDecoder();
const utf8Encoder = new TextEncoder();
export default {
name: "JSON formatter",
description: "Formats JSON.",
version: "1.0.0",
options: [indentSizeOption],
load(context) {
context.addEventListener("transform", (e) => {
if (e.name.endsWith(".json")) {
const data = utf8Decoder.decode(e.data);
const formatted = JSON.stringify(JSON.parse(data), null, parseInt(indentSizeOption.selected, 10));
e.data = utf8Encoder.encode(formatted);
}
});
},
unload() {},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment