我想将一个json数组元素转换为node.js中的csv。我发现一些模块就像json2csv或json-csv一样,但它们并不完整。例如,json2csv仅支持平面结构,其中字段是json根的直接子节点,并且所有json对象的模式也应相同。
就我而言,我想要那样。
我想我是这样的json数组对象:
[{ "libelle" : "Projet 1", "beneficiaire" : "Mr Leroy", "nature" : "Diagnostics patrimoniaux", "phasage" : "GLOBAL", "budget": [ {"status": "BROUILLON"} ], "status" : "BROUILLON" }, { "libelle" : "Projet 2", "beneficiaire" : "Mr Leroy", "nature" : "Diagnostics patrimoniaux", "phasage" : "GLOBAL", "status" : "BROUILLON" }]
我希望将它转换为csv:
"libelle","beneficiaire","nature","phasage","budget[0].status","status" "Projet 1","Mr Leroy","Diagnostics patrimoniaux","GLOBAL","BROUILLON","BROUILLON" "Projet 2","Mr Leroy","Diagnostics patrimoniaux","GLOBAL",,"BROUILLON"
我正在寻找一个完整的节点模块来完成这项工作。如果它不存在,我会自己这样做。我想是的。
答案 0 :(得分:5)
您可以非常轻松地使用模块jsonexport
,请查看此示例:
<强>示例:强>
var jsonexport = require('jsonexport');
var contacts = [{
name: 'Bob',
lastname: 'Smith',
family: {
name: 'Peter',
type: 'Father'
}
},{
name: 'James',
lastname: 'David',
family:{
name: 'Julie',
type: 'Mother'
}
},{
name: 'Robert',
lastname: 'Miller',
family: null,
location: [1231,3214,4214]
},{
name: 'David',
lastname: 'Martin',
nickname: 'dmartin'
}];
jsonexport(contacts,function(err, csv){
if(err) return console.log(err);
console.log(csv);
});
输出:
lastname;name;family.type;family.name;nickname;location
Smith;Bob;Father;Peter;;
David;James;Mother;Julie;;
Miller;Robert;;;;1231,3214,4214
Martin;David;;;dmartin;
答案 1 :(得分:0)