Skip to content

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

Open

Description

@markericson

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.

Activity

  1. JohnBarhorst-mastery commented on Mar 28, 2024

    @JohnBarhorst-mastery

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions