我正在嘗試將 csv 文本檔案轉換為具有特定結構的 JSON 物件。我一直在使用 csvtojson nodejs 模塊,我已經非常接近了,我只需要一些幫助來進行微調。
這是我的輸入檔案的樣子:
PoHeader.hdrComment,PoHeader.poNumber,PoHeader.shipDate,PoHeader.rushOrder,PoLines.0.item,PoLines.0.itemDesc,PoLines.0.line,PoLines.0.poNumber,PoLines.0.qtyOrder
"FILL AND KILL","00100XXXXX","12/31/22","false","0000-SBAN","Henry s SFBAN Swamp Fox SFBAN",10,"00100XXXXX",1.000
"FILL AND KILL","00100XXXXX","12/31/22","false","0001-0099","Daiwa GC80 Goldcast Spincast R",20,"00100XXXXX",2.000
"FILL AND KILL","00100XXXXX","12/31/22","false","0001-1544","Daiwa MINISPIN Minispin Spinni",30,"00100XXXXX",3.000
我需要最終結果來遵循這個結構:
{
"PoHeader": {
"hdrComment": "My comment",
"orderClass": "SO",
"poNumber": "test02",
"shipDate": "06/14/18",
"rushOrder": false,
"rushOption": 1,
"adultSignature": true,
},
"PoLines": [
{
"item": "0001-3121",
"itemDesc": "Some item",
"linComment": "For John Doe",
"line": 1,
"poNumber": "test02",
"qtyOrder": 1
},
]
}
這是我獲取 JSON 物件的代碼:
csv()
.fromFile(csvFilePath)
.then((jsonObj)=>{
//console.log(jsonObj);
sendPO(jsonObj);
})
當我運行我的代碼時,這是我得到的 jsonObj:
[
{
PoHeader: {
hdrComment: 'FILL AND KILL',
poNumber: '00100XXXXX',
shipDate: '12/31/22',
rushOrder: 'false'
},
PoLines: [
{
item: '0000-SBAN',
itemDesc: 'Henry s SFBAN Swamp Fox SFBAN',
line: '10',
poNumber: '00100XXXXX',
qtyOrder: '1.000'
}
]
},
{
PoHeader: {
hdrComment: 'FILL AND KILL',
poNumber: '00100XXXXX',
shipDate: '12/31/22',
rushOrder: 'false'
},
PoLines: [
{
item: '0001-0099',
itemDesc: 'Daiwa GC80 Goldcast Spincast R',
line: '20',
poNumber: '00100XXXXX',
qtyOrder: '2.000'
}
]
},
{
PoHeader: {
hdrComment: 'FILL AND KILL',
poNumber: '00100XXXXX',
shipDate: '12/31/22',
rushOrder: 'false'
},
PoLines: [
{
item: '0001-1544',
itemDesc: 'Daiwa MINISPIN Minispin Spinni',
line: '30',
poNumber: '00100XXXXX',
qtyOrder: '3.000'
}
]
}
]
[
{
PoHeader: {
hdrComment: 'FILL AND KILL',
poNumber: '00100XXXXX',
shipDate: '12/31/22',
rushOrder: 'false'
},
PoLines: [
{
item: '0000-SBAN',
itemDesc: 'Henry s SFBAN Swamp Fox SFBAN',
line: '10',
poNumber: '00100XXXXX',
qtyOrder: '1.000'
}
]
},
{
PoHeader: {
hdrComment: 'FILL AND KILL',
poNumber: '00100XXXXX',
shipDate: '12/31/22',
rushOrder: 'false'
},
PoLines: [
{
item: '0001-0099',
itemDesc: 'Daiwa GC80 Goldcast Spincast R',
line: '20',
poNumber: '00100XXXXX',
qtyOrder: '2.000'
}
]
},
{
PoHeader: {
hdrComment: 'FILL AND KILL',
poNumber: '00100XXXXX',
shipDate: '12/31/22',
rushOrder: 'false'
},
PoLines: [
{
item: '0001-1544',
itemDesc: 'Daiwa MINISPIN Minispin Spinni',
line: '30',
poNumber: '00100XXXXX',
qtyOrder: '3.000'
}
]
}
]
[
{
PoHeader: {
hdrComment: 'FILL AND KILL',
poNumber: '00100XXXXX',
shipDate: '12/31/22',
rushOrder: 'false'
},
PoLines: [
{
item: '0000-SBAN',
itemDesc: 'Henry s SFBAN Swamp Fox SFBAN',
line: '10',
poNumber: '00100XXXXX',
qtyOrder: '1.000'
}
]
},
{
PoHeader: {
hdrComment: 'FILL AND KILL',
poNumber: '00100XXXXX',
shipDate: '12/31/22',
rushOrder: 'false'
},
PoLines: [
{
item: '0001-0099',
itemDesc: 'Daiwa GC80 Goldcast Spincast R',
line: '20',
poNumber: '00100XXXXX',
qtyOrder: '2.000'
}
]
},
{
PoHeader: {
hdrComment: 'FILL AND KILL',
poNumber: '00100XXXXX',
shipDate: '12/31/22',
rushOrder: 'false'
},
PoLines: [
{
item: '0001-1544',
itemDesc: 'Daiwa MINISPIN Minispin Spinni',
line: '30',
poNumber: '00100XXXXX',
qtyOrder: '3.000'
}
]
}
]
最明顯的問題是它回傳了 3 個相同的物件。但我真正需要的是一個具有相同 PoHeader 物件的物件和每個行專案的 PoLines 物件串列。我理想的輸出是:
{
PoHeader: {
hdrComment: 'FILL AND KILL',
poNumber: '00100XXXXX',
shipDate: '12/31/22',
rushOrder: 'false'
},
PoLines: [
{
item: '0000-SBAN',
itemDesc: 'Henry s SFBAN Swamp Fox SFBAN',
line: '10',
poNumber: '00100XXXXX',
qtyOrder: '1.000'
},
{
item: '0001-0099',
itemDesc: 'Daiwa GC80 Goldcast Spincast R',
line: '20',
poNumber: '00100XXXXX',
qtyOrder: '2.000'
},
{
item: '0001-1544',
itemDesc: 'Daiwa MINISPIN Minispin Spinni',
line: '30',
poNumber: '00100XXXXX',
qtyOrder: '3.000'
}
]
}
對此的任何幫助將不勝感激,如果需要,我很樂意分享更多代碼。
uj5u.com熱心網友回復:
似乎您想合并多行,并且根據docs,它僅適用于單行:
csvtojson 能夠將 csv 行轉換為嵌套的 JSON
所以,似乎不可能嵌套多行......
你可以在決議后自己做。
例如,按PoHeader
和分組PoLines
。這是一個快速而骯臟的解決方案:
.then((jsonObj) => {
const result = jsonObj.reduce((prev, curr) => {
prev['PoHeader'] = curr['PoHeader'];
if (prev['PoLines'] && prev['PoLines'].length > 0) {
prev['PoLines'].push(curr.PoLines[0]);
} else {
prev['PoLines'] = curr.PoLines;
}
return prev;
}, {});
console.log(result);
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/517200.html