question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fields override flatten columns

See original GitHub issue
  1. json2csv: "^5.0.5"
  2. node --version v12.18.2
  3. 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)
  1. 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 },
  },
]
  1. 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 the info field, because it is “destructured” into other fields by flatten, those transformed fields won’t be included in my csv because I didn’t specify them in the fields portion. So if I remove one column from my fields, still not being the one with the nested json like fields =[ 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 by flatten

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:closed
  • Created 3 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
tlowandecommented, Dec 11, 2020

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

1reaction
juanjoDiazcommented, Dec 9, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found