Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I create nested objects from a CSV line without renaming my CSV headers? #486

Open
markericson opened this issue Dec 27, 2023 · 1 comment

Comments

@markericson
Copy link

It seems like it should be possible to configure the nested object mapping using the existing header names initialization/configuration of csvtojson without renaming the source header row in the CSV input file.

@JohnBarhorst-mastery
Copy link

JohnBarhorst-mastery commented Mar 28, 2024

@markericson Did you end up finding a solution to this? I'm looking for the same. I tried using on('headers', (headers) => headers.map(transformStuffHere)) but that doesn't appear to affect the actual headers used while processing.

Update - You can use the .preFileLine() hook to transform the headers you already have by checking for index === 0
https://github.com/Keyang/node-csvtojson?tab=readme-ov-file#csv-file-line-hook

However! This seems to be a little buggy. On a test file with just one row of headers and one row of data, I was seeing both lines logging an index of 0. Adding more rows to my CSV and the issue changed. Could absolutely be user error, but just in case be wary of it mutating your rows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants