Closed aubreyzulu closed 2 years ago
I'm not sure of what you mean. Please provide an example input and the output that you would like to get in return so I can help you.
@juanjoDiaz
const { Parser, transforms: { unwind } } = require('json2csv');
const myCars = [
{
"carModel": "BMW",
"price": 15000,
"items": [
{
"name": "airbag",
"color": "white"
}, {
"name": "dashboard",
"color": "black"
}
]
}, {
"carModel": "Porsche",
"price": 30000,
"items": [
{
"name": "airbag",
"items": [
{
"position": "left",
"color": "white"
}, {
"position": "right",
"color": "gray"
}
]
}, {
"name": "dashboard",
"items": [
{
"position": "left",
"color": "gray"
}, {
"position": "right",
"color": "black"
}
]
}
]
}
];
const fields = ['carModel', 'price', 'items.name', 'items.color', 'items.items.position', 'items.items.color'];
const transforms = [unwind({ paths: ['items', 'items.items'] })];
const json2csvParser = new Parser({ fields, transforms });
const csv = json2csvParser.parse(myCars);
console.log(csv);
Output
"BMW",15000,"airbag","white",,
"BMW",15000,"dashboard","black",,
"Porsche",30000,"airbag",,"left","white"
"Porsche",30000,"airbag",,"right","gray"
"Porsche",30000,"dashboard",,"left","gray"
"Porsche",30000,"dashboard",,"right","black"
**Expected Out Put**
"BMW",15000,"airbag","white",dashboard,black,
"Porsche",30000,"airbag",,"left","white,"airbag,right,gray,dashboard,left,gray,dashboard,right,black"
I don't want to repeat data for the parent object, instead, I want to append data for nested array objects
You don't need unwind
then.
You need flatten
. 🙂
const transforms = [flatten({ arrays: true })];
const json2csvParser = new Parser({ transforms });
const csv = json2csvParser.parse(myCars);
outputs
"carModel","price","items.0.name","items.0.color","items.1.name","items.1.color","items.0.items.0.position","items.0.items.0.color","items.0.items.1.position","items.0.items.1.color","items.1.items.0.position","items.1.items.0.color","items.1.items.1.position","items.1.items.1.color"
"BMW",15000,"airbag","white","dashboard","black",,,,,,,,
"Porsche",30000,"airbag",,"dashboard",,"left","white","right","gray","left","gray","right","black"
You don't need
unwind
then. You needflatten
. 🙂const transforms = [flatten({ arrays: true })]; const json2csvParser = new Parser({ transforms }); const csv = json2csvParser.parse(myCars);
outputs
"carModel","price","items.0.name","items.0.color","items.1.name","items.1.color","items.0.items.0.position","items.0.items.0.color","items.0.items.1.position","items.0.items.1.color","items.1.items.0.position","items.1.items.0.color","items.1.items.1.position","items.1.items.1.color" "BMW",15000,"airbag","white","dashboard","black",,,,,,,, "Porsche",30000,"airbag",,"dashboard",,"left","white","right","gray","left","gray","right","black"
@juanjoDiaz Thanks, is there a way of accessing the values (fields) dynamically?
Sorry @Aubrey-Zulu I totally missed your last message.
Did you figure it out?
I do not really understand what you mean by accessing fields dynamically. If you mean having accesson function, just read the doc for the fields
option
Is there a way of appending data to the end of a CSV instead of creating a new row for the data after unwinding..? refer docs: The unwind transform deconstructs an array field from the input item to output a row for each element.