Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

CouchDB JSON to CSV view converter use ?include_docs=true&reduce=false

View couchdb csv list
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
{
"csv":"function(head, req){
start({
'headers': {
'Content-Type': 'text/csv'
 
}
});
Array.prototype.unique = function() {
var a = this.concat();
for(var i=0; i<a.length; ++i) {
for(var j=i+1; j<a.length; ++j) {
if(a[i] === a[j])
a.splice(j, 1);
}
}
 
return a;
};
var getheaders = function (name,value,parent) {
var ret = \"\";
if (typeof(value) == \"object\") {
for (var x in value){
var family = name;
if (parent!=\"\") family = parent+\".\"+name;
ret = ret + getheaders(x,value[x],family);
}
} else {
if (!isNaN(name)){
ret = ret + parent+\"[\"+name+\"],\";
}else{
if (parent!=\"\"){
ret = ret + parent+\".\"+name+\",\";
}else{
ret = ret + name+\",\";
}
}
}
return ret;
};
var tocsv = function(therow,heads){
ret = '';
for (var h in heads){
prop = heads[h].toString().replace(/\\[/gi,\"'][\");
prop = \"['\"+prop.replace(/\\./gi,\"']['\");
if (prop.charAt(prop.length-1)!=']') prop = prop+\"']\";
cmd = 'therow'+prop;
try{
valor = eval(cmd);
valor = valor.toString().replace(/,/gi,' ');
valor = valor.toString().replace(/(\\r\\n|\\n|\\r)/gm,'');
}
catch(er){
valor = '';
}
ret = ret + valor + ',';
}
return ret;
};
var row; var rows=[];
var headers=[];
var muestra = 100;
var cnt = 1;
var yasalio = false;
while(row = getRow()){
if (cnt < muestra){
header = getheaders(\"\",row,\"\");
header = (header.charAt(header.length-1)==',')?header.slice(0, -1):header;
header = header.split(',');
headers = headers.concat(header).unique();
rows.push(row);
}else{
if (cnt==muestra){
send(headers.join()+\"\\n\");
for (var x in rows){
send(tocsv(rows[x],headers) +\"\\n\");
}
}
send(tocsv(row,headers) +\"\\n\");
yasalio = true;
}
cnt = cnt + 1;
}
if (!yasalio){
send(headers.join()+\"\\n\");
for (var x in rows){
send(tocsv(rows[x],headers) +\"\\n\");
}
}
return \"\";
}"
}

Great!

Doesn't work for me. Some syntax error probably.

Hey,

I tried using your code, but it seems that there are several syntax problem in there, were you able to fix it?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.