-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
34 lines (31 loc) · 1.35 KB
/
script.js
File metadata and controls
34 lines (31 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const stringifyBtn = document.getElementById('stringify-btn');
const destringifyBtn = document.getElementById('destringify-btn');
const jsonInput = document.getElementById('json-input');
const jsonOutput = document.getElementById('json-output');
const copyBtn = document.getElementById('copy-btn');
stringifyBtn.addEventListener('click', () => {
try {
const jsonObject = JSON.parse(jsonInput.value);
const jsonString = JSON.stringify(jsonObject, (key, value) => value);
const escapedJsonString = jsonString.replace(/"/g, '\\"');
jsonOutput.value = '"' + escapedJsonString + '"';
} catch (error) {
jsonOutput.value = 'Invalid JSON input!';
}
});
destringifyBtn.addEventListener('click', () => {
try {
const jsonString = jsonInput.value;
const unescapedJsonString = jsonString.replace(/\\"/g, '"'); // Unescape double quotes
const unquotedJsonString = unescapedJsonString.replace(/^"|"$/g, ''); // Remove surrounding quotes
const jsonObject = JSON.parse(unquotedJsonString);
jsonOutput.value = JSON.stringify(jsonObject, null, 2);
} catch (error) {
jsonOutput.value = 'Invalid JSON input!';
}
});
copyBtn.addEventListener('click', () => {
const outputTextarea = document.getElementById('json-output');
outputTextarea.select();
document.execCommand('copy');
});