fields override flatten columns
See original GitHub issuejson2csv: "^5.0.5"
node --version v12.18.2
- Include the command or code you used.
const { Parser, transforms: { flatten } } = require('json2csv')
const json2csvParser = new Parser({
fields,
transforms: [
flatten({
separator: '_',
objects: true,
arrays: true,
}),
],
})
const csv = json2csvParser.parse(mydata)
- Include a sample dataset that we can test against.
[
{
new_cases: 1,
total_cases: 118,
province: 'New Brunswick',
info: [
{
Mon: '0',
Mon1: '0',
Mon2: '0',
Mon3: '0',
Mon4: '0',
},
{ Tue: '1' },
{ Wed: '1' },
{ Thu: '3' },
{ Fri: '2' },
],
},
{
new_cases: 1,
total_cases: 26,
province: 'PEI',
info:{
Mon: '0',
Tue: '1',
Wed: '1',
Thu: '3',
Fri: '2',
},
},
{
new_cases: 1,
total_cases: 5,
province: 'Northwest Territories',
info: { 0: 29, 1: 18, 2: 11, 3: 7, 4: 12, 5: 23, 6: 58, 7: 72, 8: 78, 9: 81, 10: 80, 11: 65, 12: 66, 13: 61, 14: 57, 15: 52, 16: 44, 17: 49, 18: 40, 19: 50, 20: 59, 21: 68, 22: 55, 23: 45 },
},
]
- Include your output/error.
not really an error, more like a behavior question: if my fields are specified, the flatten fields are not included.
If I say my
fields =[ new_cases, total_cases, province, info ]
(so all of them), even though I included theinfo
field, because it is “destructured” into other fields byflatten
, those transformed fields won’t be included in my csv because I didn’t specify them in thefields
portion. So if I remove one column from my fields, still not being the one with the nested json likefields =[ total_cases, province, info ]
the same happens, I get my info field blank and I don’t get the “destructured” columns from the nested json generated byflatten
Is there something I’m doing wrong or an option I should be using? I’d like to be able to choose the columns I wanna include in my csv but if the chosen column has a nested json and I have passed the flatten
option, I’d like the transformed fields to be included in my csv.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7
Top Results From Across the Web
How to flatten Rows into Column (Field) Values - Stack Overflow
You can use aggregation: select job_id, max(team1) team1, max(team2) team2, max(team3) team3 from mytable group by job_id.
Read more >flatten Parameters - Salesforce Help
When you define a flatten transformation, you set the action attribute to flatten and specify the parameters. The following table describes the input...
Read more >Flattened field type | Elasticsearch Guide [8.5] | Elastic
The flattened type provides an alternative approach, where the entire object is mapped as a single field. Given an object, the flattened mapping...
Read more >Flattening Fields - Informatica Documentation
Flatten fields of a complex data type to modify hierarchical data or to convert to relational data.
Read more >Flattening and renaming Spark Dataframe having a complex ...
To change the names of nested columns, there are some options: By building a new struct column on the flight with the struct()...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
oh my 🤦♀️ , you’re ARE right! I read that so many times but I couldn’t combine it with the flatten concept in my mind, since it generates those columns on the go and my JSON values don’t all have the same structure. Thanks for an awesome and prompt reply
Yup. It should work just the same as the sync parser.
I just run your code with your code sample and I got the flattened CSV as expected.