-
Notifications
You must be signed in to change notification settings - Fork 16
/
table to json.html
70 lines (59 loc) · 2.46 KB
/
table to json.html
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Table to json converter</title>
<style>
textarea {
width: 600px;
height: 350px;
}
</style>
<script>
function convert() {
text = document.getElementById("input1").value;
if (document.getElementById("int1").checked) {
text = text.replace(/^/gm, '"');
text = text.replace(/\t|,|, /gm, '": ');
text = text.replace(/$/gm, ',');
} else {
text = text.replace(/^/gm, '"');
text = text.replace(/\t|,|, /gm, '": "');
text = text.replace(/$/gm, '",');
}
text = text.replace(/^""(: ,|: "",)$/gm, '},');
text = text.replace(/(: ,|: "",)$/gm, ': {');
text = text.replace(/,\n(?!")/gm, '\n');
text = text.substr(0, text.length - 1);
document.getElementById("output1").value = text;
}
</script>
</head>
<body>
<h1>Table to json thingy</h1>
<pre>If you copy and pase a 2-column table from google sheets, excel or similar, this will crudely attempt to parse this to JSON key:value pairs in order to more conveniently create block property lists.</pre>
<pre>Relies on columns being separated by tab characters (	) or commas ("," or ", "), and rows being separated with newlines.</pre>
<pre>Also supports nested tables for properties with different values for different variants when using some fancier formatting that I can't be arsed to explain in this paragraph.</pre>
<br>
<textarea onkeyup="convert();" id="input1"></textarea>
<input type="checkbox" id="int1" oninput="convert();">Int?</input>
<textarea id="output1"></textarea>
<br>
<script>
function convertReverseTable() {
text = document.getElementById("input2").value;
var output = "";
text.split('\n').forEach(row => {
columns = row.split(/, |\t/);
value = columns.shift();
for (let i in columns) {
if (columns[i] != '') output += `"${columns[i]}": "${value}",\n`;
}
;
});
document.getElementById("output2").value = output.substr(0, output.length - 2);
}
</script>
<textarea onkeyup="convertReverseTable();" id="input2"></textarea>
<textarea id="output2"></textarea>
</body>
</html>