I created a helper a few months back that used DATA URIs to download JSON to CSV, but due to IE’s implementation of DATA URIs (or lack of), it does not work for IE (all versions). Here is the same helper that will just convert the data, which you can use anyway you want (example: in a popup, to display in a modal window, etc…).
<html>
<head>
<title>Demo - Covnert JSON to CSV</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="https://github.com/douglascrockford/JSON-js/raw/master/json2.js"></script>
<script type="text/javascript">
// JSON to CSV Converter
function ConvertToCSV(objArray) {
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
if (line != '') line += ','
line += array[i][index];
}
str += line + '\r\n';
}
return str;
}
// Example
$(document).ready(function () {
// Create Object
var items = [
{ name: "Item 1", color: "Green", size: "X-Large" },
{ name: "Item 2", color: "Green", size: "X-Large" },
{ name: "Item 3", color: "Green", size: "X-Large" }];
// Convert Object to JSON
var jsonObject = JSON.stringify(items);
// Display JSON
$('#json').text(jsonObject);
// Convert JSON to CSV & Display CSV
$('#csv').text(ConvertToCSV(jsonObject));
});
</script>
</head>
<body>
<h1>
JSON</h1>
<pre id="json"></pre>
<h1>
CSV</h1>
<pre id="csv"></pre>
</body>
</html>
Here is the output using the script and above.
JSON
Created with “JSON.stringify()” function from json.org.
[{"name":"Item 1","color":"Green","size":"X-Large"},{"name":"Item 2","color":"Green","size":"X-Large"},{"name":"Item 3","color":"Green","size":"X-Large"}]
CSV
Created with “ConvertToCSV()” function I created above.
Item 1,Green,X-Large Item 2,Green,X-Large Item 3,Green,X-Large





#1 by Dennis on June 21, 2011 - 5:32 pm
Quote
Super, thanks for posting! Two fixes though: 1. Need closing ] in items object; 2. should be closing pre tag
#2 by Zach on June 21, 2011 - 5:44 pm
Quote
Dennis, thanks for pointing that out… Something weird must have happened with my copy and paste or my fast typing fingers. Fixed.
#3 by Ilya on August 2, 2011 - 9:54 am
Quote
// Convert Oject to JSON
Missing “b” in Object
#4 by Zach on August 2, 2011 - 10:38 am
Quote
thanks, i’ll correct the typo…
#5 by Guus Beltman on August 9, 2011 - 3:50 am
Quote
Zach, thanks for sharing this nifty piece of jQuery. For CSV it is also common that the 1st line if mentioning the column names. You make it work when you insert this snippet just after ‘var str = ”;’ :
for (var colname in array[0]) {
if (str != ”) str += ‘,’
str += colname;
}
str += ‘\r\n’;
Kind regards,
Guus
#6 by Ethan Pepper on January 2, 2012 - 2:59 pm
Quote
Yes, how would this work if instead of creating an object you were getting a JSON object froma URL. How do you call the URL and pass it?
#7 by Zach on January 3, 2012 - 12:57 pm
Quote
Ethan,
You call the URL using the normal jQuery getJSON() or ajax() method. Once you download the object, you then transform it on your page using the helper above. Here is the official jQuery documentation on how to call a service via ajax(): http://api.jquery.com/jQuery.ajax/
#8 by Yiannis on January 14, 2012 - 10:45 am
Quote
HEllo
could you please give me detailed instructions how to use this code ?
thanx
Trackback: Informasi Online Organisasi Wong Jawa Kelahiran di Sumatera