Skip to content

Instantly share code, notes, and snippets.

@chrisfsmith
Created March 29, 2023 18:59
Show Gist options
  • Save chrisfsmith/eb0c37266ffa27cb2f7f4956d729c033 to your computer and use it in GitHub Desktop.
Save chrisfsmith/eb0c37266ffa27cb2f7f4956d729c033 to your computer and use it in GitHub Desktop.
Boop script to remove leading/trailing quotes, unescapes, and json formats your text
/**
{
"api":1,
"name":"Swarm",
"description":"Removes leading/trailing quotes, unescapes, and json formats your text",
"author":"Chris",
"icon":"broom",
"tags":"remove,slashes,escape,json,prettify,clean,indent"
}
**/
function main(input) {
input.text = (input.text + '')
.replace(/^"(.*)$/, '$1')
.replace(/^(.*)"$/, '$1')
.replace(/\\(.?)/g, function (s, n1) {
switch (n1) {
case '\\':
return '\\'
case '0':
return '\u0000'
case '':
return ''
default:
return n1
}
});
try {
input.text = JSON.stringify(JSON.parse(input.text), null, 2);
} catch (error) {
input.postError("Invalid JSON")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment