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

Parse dot notation columns into nested objects #414

Open
jivung opened this issue Jan 24, 2024 · 4 comments
Open

Parse dot notation columns into nested objects #414

jivung opened this issue Jan 24, 2024 · 4 comments

Comments

@jivung
Copy link

jivung commented Jan 24, 2024

I want to be able to write my column names with dot notation, like this:

name.first,name.last,age
john,doe,25

The parsing should result in this:

[
  {
     name: {
       first: 'john',
       last: 'doe'
     },
     age: 25
   }
]

I dont know if it's already possible and I just dont understand how to do it.

@wdavidw
Copy link
Member

wdavidw commented Jan 24, 2024

The closest thing is the column option but it doesnt support (yet?) nested column.

@datu925
Copy link

datu925 commented Jan 31, 2024

+1 to this, this would be a great feature.

In the archived repo, this was requested here: adaltas/node-csv-parse#76.

And I came here to make a similar suggestion. I'm rolling my own simple version right now but if this was part of the library, even better.

I think the argument for it is that csv-stringify offers this nested properties behavior when outputting, so being able to read that format when parsing (enabling a "round trip") is desirable.

@wdavidw
Copy link
Member

wdavidw commented Jan 31, 2024

Yes, I remember a few years back porting the underscore code into the library to support this feature without including an external dependency. A little buzy in the next copple of weeks between work and then holidays but I'll try to find the time.

@datu925
Copy link

datu925 commented Apr 17, 2024

In case it helps as a starting point, my implementation was something like the below (I've edited it a bit to remove project-specific things, so it may no longer compile). NestedKeyVal was a custom type that is supposed to represent a possibly nested string-keyed object.

export function flatToNested(
  rows: any[],
)  {
  const outputs: NestedKeyVal[] = [];
  for (const row of rows) {
    const output: NestedKeyVal = {};
    for (const columnName in row) {
      let val = row[columnName];

      const chunks = columnName.split('.');
      // dest is basically a pointer to a particular object or subobject in
      // the output structure. It starts at the root output for each key but
      // advances into sub-objects to allow nesting.
      let dest = output;
      if (chunks.length === 1) {
        dest[chunks[0]] = val;
      } else {
        for (const chunk of chunks.slice(0, -1)) {
          if (!(chunk in dest)) {
            const subobj: NestedKeyVal = {};
            dest[chunk] = subobj;
          }
          dest = dest[chunk];
        }
        dest[chunks.at(-1)!] = val;
      }
    }
    outputs.push(output);
  }
  return outputs;
}

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

No branches or pull requests

3 participants