如何在 NodeJS 中将 JSON 转换为 CSV,使其不包含 JSON 的键值作为第一行?
How do i convert JSON to CSV in NodeJS in a way that it does not contain the key values of JSON as first row?
我的输入:
const jsonData = [{
name : "Himank",
age : 20
},{
name : "Manisha",
age : 20
},{
name : "Sourav",
age : 20
}]
预期输出(使用 json2csv 或 josn-2-csv 包)):
Himank, 20
Manisha, 20
Sourav, 20
header - Boolean, determines whether or not CSV file will contain a title column. Defaults to true if not specified.
;(()=> {
const jsonData = [{
name : "Himank",
age : 20
},{
name : "Manisha",
age : 20
},{
name : "Sourav",
age : 20
}];
const fields = ['name', 'age'];
const opts = { fields, header: false };
try {
const {Parser } = json2csv
const parser = new Parser(opts);
const csv = parser.parse(jsonData);
console.log(csv);
} catch (err) {
console.error(err);
}
})()
;(()=> {
const jsonData = [{
name : "Himank",
age : 20
},{
name : "Manisha",
age : 20
},{
name : "Sourav",
age : 20
}];
const fields = ['name', 'age'];
const opts = { fields, header: false };
try {
const {Parser } = json2csv
const parser = new Parser(opts);
const csv = parser.parse(jsonData);
console.log(csv);
} catch (err) {
console.error(err);
}
})()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://cdn.jsdelivr.net/npm/json2csv@4.2.1"></script>
</head>
<body>
</body>
</html>
我的输入:
const jsonData = [{
name : "Himank",
age : 20
},{
name : "Manisha",
age : 20
},{
name : "Sourav",
age : 20
}]
预期输出(使用 json2csv 或 josn-2-csv 包)):
Himank, 20
Manisha, 20
Sourav, 20
header - Boolean, determines whether or not CSV file will contain a title column. Defaults to true if not specified.
;(()=> {
const jsonData = [{
name : "Himank",
age : 20
},{
name : "Manisha",
age : 20
},{
name : "Sourav",
age : 20
}];
const fields = ['name', 'age'];
const opts = { fields, header: false };
try {
const {Parser } = json2csv
const parser = new Parser(opts);
const csv = parser.parse(jsonData);
console.log(csv);
} catch (err) {
console.error(err);
}
})()
;(()=> {
const jsonData = [{
name : "Himank",
age : 20
},{
name : "Manisha",
age : 20
},{
name : "Sourav",
age : 20
}];
const fields = ['name', 'age'];
const opts = { fields, header: false };
try {
const {Parser } = json2csv
const parser = new Parser(opts);
const csv = parser.parse(jsonData);
console.log(csv);
} catch (err) {
console.error(err);
}
})()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://cdn.jsdelivr.net/npm/json2csv@4.2.1"></script>
</head>
<body>
</body>
</html>