-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
155 lines (125 loc) · 5.23 KB
/
index.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Firestore Compatible JSON Converter</title>
<!-- Bootstrap CSS link -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<style>body {
background-color: #f8f9fa;
display: flex;
flex-direction: column;
min-height: 100vh;
}
.container {
margin-top: 30px;
max-width: 600px;
flex: 1;
padding-bottom: 120px; /* Adjust this value based on your footer height */
}
textarea {
resize: vertical;
}
button {
margin-top: 10px;
}
footer {
background-color: #343a40;
color: white;
text-align: center;
padding: 10px;
position: fixed;
bottom: 0;
width: 100%;
z-index: 1;
}
footer a {
color: white;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<h1 class="display-4">Firestore Compatible JSON Converter</h1>
<label for="jsonInput">Paste JSON or Select a File:</label>
<textarea id="jsonInput" class="form-control" rows="10" cols="50"></textarea><br>
<div class="custom-file">
<input type="file" class="custom-file-input" id="fileInput" accept=".json">
<label class="custom-file-label" for="fileInput">Choose file</label>
</div><br>
<button onclick="processJson()" class="btn btn-primary">Convert to Firestore Compatible JSON</button><br>
<label for="output">Result:</label>
<textarea id="output" class="form-control" rows="10" cols="50" readonly></textarea><br>
<button onclick="saveToFile()" class="btn btn-success">Save to File</button>
</div>
<footer>
<p>This tool is powered by <a href="https://github.com/firebase-me/firestore-json-sanitizer"
target="_blank">Firestore JSON Sanitizer</a>.</p>
</footer>
<!-- Bootstrap JS and Popper.js (required for Bootstrap) -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script>
function convertToFirestoreCompatible(jsonString) {
let jsonObject = JSON.parse(jsonString);
function sanitizeKey(key) {
// Replace accented characters with their basic counterparts
let result = key.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
// Trim leading and trailing spaces
result = result.trim();
// Replace spaces with underscores and sanitize other characters
result = result.replace(/[.#$/\[\] ]/g, '_');
return result;
}
function iterateObject(obj) {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
const sanitizedKey = sanitizeKey(key);
// If the key was changed, update it
if (sanitizedKey !== key) {
obj[sanitizedKey] = obj[key];
delete obj[key];
}
// If the value is an object, recursively iterate through it
if (typeof obj[sanitizedKey] === 'object' && obj[sanitizedKey] !== null) {
iterateObject(obj[sanitizedKey]);
}
}
}
}
iterateObject(jsonObject);
return JSON.stringify(jsonObject, null, 2); // Use null, 2 for pretty-printing
}
function processJson() {
const jsonInput = document.getElementById('jsonInput').value;
const fileInput = document.getElementById('fileInput');
if (fileInput.files.length > 0) {
const file = fileInput.files[0];
const reader = new FileReader();
reader.onload = function (e) {
const fileJson = e.target.result;
const firestoreCompatibleJson = convertToFirestoreCompatible(fileJson);
document.getElementById('output').value = firestoreCompatibleJson;
};
reader.readAsText(file);
} else {
const firestoreCompatibleJson = convertToFirestoreCompatible(jsonInput);
document.getElementById('output').value = firestoreCompatibleJson;
}
}
function saveToFile() {
const outputJson = document.getElementById('output').value;
const blob = new Blob([outputJson], { type: 'application/json' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'converted.json';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
</script>
</body>
</html>