Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script to remove escape characters from JSON string #347

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Scripts/UnescapeJSON.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
{
"api":1,
"name":"Unescape JSON String",
"description":"Removes escape characters from JSON string",
"author":"tahsinature",
"icon":"table",
"tags":"json,unescape,escape"
}
**/

const unescapeJSONString = (str = "") => {
return str
.replace(/(\\n)/g, "")
.replace(/(\\r)/g, "")
.replace(/(\\t)/g, "")
.replace(/(\\f)/g, "")
.replace(/(\\b)/g, "")
.replace(/(\")/g, '"')
.replace(/("{)/g, "{")
.replace(/(}")/g, "}")
.replace(/(\\)/g, "")
.replace(/(\/)/g, "/");
};

function main(state) {
let input = state.fullText;
try {
try {
JSON.parse(input);
} catch (error) {
if (!input.startsWith('"')) input = `"${input}`;
if (!input.endsWith('"')) input = `${input}"`;
JSON.parse(input);
}

state.fullText = unescapeJSONString(input);
state.postInfo("JSON is unescapped!");
} catch (error) {
let msg = "Something went wrong!";

if (error instanceof SyntaxError) msg = "Invalid JSON";
else msg = `${error.name}-${error.message}`;

state.postError(msg);
}
}