Created
June 30, 2012 23:28
-
-
Save xalakox/3026004 to your computer and use it in GitHub Desktop.
CouchDB JSON to CSV view converter use ?include_docs=true&reduce=false
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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 \"\"; | |
}" | |
} |
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!
Working fine with me, Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great!