-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-stringify.html
More file actions
28 lines (27 loc) · 822 Bytes
/
json-stringify.html
File metadata and controls
28 lines (27 loc) · 822 Bytes
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
<style>
html textarea {
min-height: 500px;
width: 50%;
margin: 0 10px;
font-family: monospace;
}
</style>
<script>
function convert (val) {
document.querySelector('#b').value = JSON.stringify(val.trim())
}
function convert2 (val) {
try {
// negative lookahead to make sure not unescape string content
const sanitized = val.replace(/(?<!\\)\\"/g,'"').replace(/(?<!\\)\\n/g, ' ')
document.querySelector('#a').value = JSON.stringify(JSON.parse(sanitized), null, 2)
} catch (e ) {
console.log(e)
}
}
</script>
JSON stringify<br/>
<textarea name="" id="a" cols="30" rows="10" onchange="convert(this.value)"></textarea>
<br/>
JSON parse (and unescape)<br/>
<textarea name="" id="b" cols="30" rows="10" onchange="convert2(this.value)"></textarea>