-
Notifications
You must be signed in to change notification settings - Fork 211
/
index.html
424 lines (350 loc) · 11.8 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<!doctype html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>JSON to CSV Converter</title>
<meta name="og:title" content="JSON to CSV Converter" />
<meta name="description" content="A fast, private JSON-to-CSV converter. Your data is never shared with our servers." />
<meta name="og:description" content="A fast, private JSON-to-CSV converter. Your data is never shared with our servers." />
<meta name="author" content="Eric Mill" />
<meta name="twitter:creator" content="@konklone" />
<meta name="twitter:creator:id" content="5232171">
<meta name="twitter:site" content="@konklone">
<meta name="twitter:site:id" content="5232171">
<meta name="twitter:url" content="https://konklone.io/json/" />
<link rel="shortcut icon" href="/json/favicon.png" />
<link rel="canonical" href="https://konklone.io/json/" />
<!-- JSON5 parser -->
<script src='assets/json5-2.1.0.min.js'></script>
<!-- jquery, jquery-csv,bootstrap -->
<script src='assets/jquery-2.1.1.min.js'></script>
<script src="assets/jquery.csv.js"></script>
<link href="assets/bootstrap.min.css" type="text/css" rel="stylesheet" />
<!-- site styles and JS -->
<link href="assets/site.css" type="text/css" rel="stylesheet" />
<link href="assets/github.css" type="text/css" rel="stylesheet" />
<script src="assets/site.js?123456"></script>
<script src="assets/highlight.pack.js"></script>
<script>
var excerptRows = 7;
var input;
var separator;
var url;
function doJSON() {
// just in case
$(".drop").hide();
// get input JSON, try to parse it
var newInput = $(".json textarea").val();
var newSeparator = getSeparator();
if (newInput == input && newSeparator == separator) return;
input = newInput;
if (!input) {
// wipe the rendered version too
$(".json code").text("");
return;
}
separator = newSeparator;
var json = jsonFrom(input);
// if succeeded, prettify and highlight it
// highlight shows when textarea loses focus
if (json) {
// Reset any error message from previous failed parses.
$("div.error").hide();
$("div.warning").show();
var pretty = JSON.stringify(json, undefined, 2);
$(".json code").text(pretty);
if (pretty.length < (50 * 1024))
hljs.highlightBlock($(".json code").get(0));
// convert to CSV, make available
doCSV(json);
} else {
// Show error.
$("div.warning").hide();
$("div.error").show();
$(".json code").text("");
}
return true;
}
function renderAll(e) {
var json = jsonFrom(input);
var inArray = arrayFrom(json);
var outArray = [];
for (var row in inArray)
outArray[outArray.length] = parse_object(inArray[row]);
renderCSV(outArray);
$(".show-render-all").hide();
return false;
}
// show rendered JSON
function showJSON(rendered) {
console.log("ordered to show JSON: " + rendered);
if (rendered) {
if ($(".json code").text()) {
console.log("there's code to show, showing...");
$(".json .rendered").show();
$(".json .editing").hide();
}
} else {
$(".json .rendered").hide();
$(".json .editing").show().focus();
}
}
function showCSV(rendered) {
if (rendered) {
if ($(".csv table").text()) {
$(".csv .rendered").show();
$(".csv .editing").hide();
}
} else {
$(".csv .rendered").hide();
$(".csv .editing").show().focus();
}
}
function getSeparator() {
var separator = $(".json .separator select").val();
switch (separator) {
case "tab":
return "\t";
break;
case "semicolon":
return ';';
break;
case "comma":
default:
return ',';
break;
}
}
// takes an array of flat JSON objects, converts them to arrays
// renders them into a small table as an example
function renderCSV(objects) {
var rows = $.csv.fromObjects(objects, {justArrays: true});
if (rows.length < 1) return;
// find CSV table
var table = $(".csv table")[0];
$(table).text("");
// render header row
var thead = document.createElement("thead");
var tr = document.createElement("tr");
var header = rows[0];
for (field in header) {
var th = document.createElement("th");
$(th).text(header[field])
tr.appendChild(th);
}
thead.appendChild(tr);
// render body of table
var tbody = document.createElement("tbody");
for (var i=1; i<rows.length; i++) {
tr = document.createElement("tr");
for (field in rows[i]) {
var td = document.createElement("td");
$(td)
.text(rows[i][field])
.attr("title", rows[i][field]);
tr.appendChild(td);
}
tbody.appendChild(tr);
}
table.appendChild(thead);
table.appendChild(tbody);
}
function doCSV(json) {
// 1) find the primary array to iterate over
// 2) for each item in that array, recursively flatten it into a tabular object
// 3) turn that tabular object into a CSV row using jquery-csv
var inArray = arrayFrom(json);
var outArray = [];
for (var row in inArray)
outArray[outArray.length] = parse_object(inArray[row]);
$("span.rows.count").text("" + outArray.length);
var csv = $.csv.fromObjects(outArray, {separator: getSeparator()});
// excerpt and render first few rows
renderCSV(outArray.slice(0, excerptRows));
showCSV(true);
// if there's more we're not showing, add a link to show all
if (outArray.length > excerptRows)
$(".show-render-all").show();
// show raw data if people really want it
$(".csv textarea").val(csv);
// download link to entire CSV as data
// thanks to https://jsfiddle.net/terryyounghk/KPEGU/
// and https://stackoverflow.com/questions/14964035/how-to-export-javascript-array-info-to-csv-on-client-side
var uri = "data:text/csv;charset=utf-8," + encodeURIComponent(csv);
$(".csv a.download").attr("href", uri);
}
// Load any (now-deprecated) permalinks. We can't save them anymore,
// but old links can still work.
function loadPermalink() {
var id = getParam("id");
if (!id) return;
$.get('https://api.github.com/gists/' + id,
function(data, status, xhr) {
console.log("Remaining this hour: " + xhr.getResponseHeader("X-RateLimit-Remaining"));
var input = data.files["source.json"].content;
$(".json textarea").val(input);
doJSON();
showJSON(true);
}
).fail(function(xhr, status, errorThrown) {
console.log("Error fetching anonymous gist!");
console.log(xhr);
console.log(status);
console.log(errorThrown);
});
}
$(function() {
$(".json textarea").blur(function() {showJSON(true);});
$(".json pre").click(function() {showJSON(false)});
$(".json .separator select").change(function() {doJSON();});
$(".csv textarea").blur(function() {showCSV(true);})
$(".csv .raw").click(function() {
showCSV(false);
$(".csv textarea").focus().select();
return false;
})
$(".render-all").click(renderAll);
// if there's no CSV to download, don't download anything.
// also, log an analytics event.
$(".csv a.download").click(function() {
var data = $(".csv textarea").val();
if (data) {
Events.download(data.length);
return true;
} else
return false;
});
// transform the JSON whenever it's pasted/edited
$(".json textarea")
.on('paste', function() {
// delay the showing so the paste is pasted by then
setTimeout(function() {
doJSON();
$(".json textarea").blur();
}, 1);
})
.keyup(doJSON); // harmless to repeat doJSON
// go away
$("body").click(function() {
$(".drop").hide();
});
$(document)
.on("dragenter", function(e) {
e.preventDefault();
e.stopPropagation();
$(".drop").show();
})
.on("dragover", function(e) {
e.preventDefault();
e.stopPropagation();
})
.on("dragend", function(e) {
e.preventDefault();
e.stopPropagation();
$(".drop").hide();
})
.on("drop", function(e) {
$(".drop").hide();
if (e.originalEvent.dataTransfer) {
if (e.originalEvent.dataTransfer.files.length) {
e.preventDefault();
e.stopPropagation();
var reader = new FileReader();
reader.onload = function(ev) {
console.log(ev.target.result);
$(".json textarea").val(ev.target.result);
setTimeout(function() {
doJSON();
$(".json textarea").blur();
}, 1);
}
reader.readAsText(e.originalEvent.dataTransfer.files[0]);
}
}
});
// highlight CSV on click
$(".csv textarea").click(function() {$(this).focus().select();});
// Support (now-deprecated) anonymous gist-backed permalinks.
loadPermalink();
});
</script>
</head>
<body>
<h1>Convert JSON to CSV</h1>
<section class="json">
<p>
<span class="instruction editing">
To convert JSON to CSV, paste your JSON below. Your data is never sent to our servers.
</span>
<span class="instruction rendered">
Click your JSON below to edit.
</span>
<span>
Please <a target="_blank" href="https://github.com/konklone/json/issues">report bugs and send feedback</a> on GitHub.
</span>
<span>
Made by <a href="https://konklone.com">Eric Mill</a> (<a href="https://twitter.com/konklone">@konklone</a>).
</span>
</p>
<div class="areas">
<textarea class="editing"></textarea>
<pre class="rendered"><code></code></pre>
<div class="drop">DROP JSON HERE</div>
</div>
<div class="separator">
Separator
<select name="separator">
<option value="comma" selected>Comma</option>
<option value="tab">Tab</option>
<option value="semicolon">Semicolon</option>
</select>
</div>
<div class="warning">
Since the conversion is done inside your browser, extremely large files may cause trouble. Microsoft Edge has a smaller limit than other browsers.
</div>
<div class="error">
There was an error parsing this JSON. If you're sure this JSON is valid, please
<a class="report" target="_blank"
href="https://github.com/konklone/json/issues/new">
file an issue</a>.
</div>
</section>
<section class="csv">
<p>
<span class="rendered">
Below are the first few rows (<span class="rows count"></span> total).
<a download="result.csv" href="#" class="download">
Download the entire CSV</a>,
<span class="show-render-all">
<a href="#" class="render-all">show all <span class="rows count"></span> rows</a>,
</span>
or <a href="#" class="raw">show the raw data</a>.
</span>
<span class="editing">
Your JSON will appear below as a table.
</span>
</p>
<div class="areas">
<textarea class="editing" readonly></textarea>
<div class="table rendered">
<table></table>
</div>
</div>
</section>
<footer>
<p>
Thanks to <a href="https://twitter.com/benbalter">@benbalter</a> for help, and to <a href="https://twitter.com/onyxfish">@onyxfish</a> for the amazing <a href="https://csvkit.readthedocs.org/en/latest/scripts/in2csv.html">csvkit</a>.
</p>
</footer>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-252618-16', 'konklone.io');
ga('set', 'forceSSL', true);
ga('set', 'anonymizeIp', true);
ga('send', 'pageview');
</script>
</body>
</html>