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

Problem with list of objects same keys #207

Closed
4 of 5 tasks
mebibou opened this issue Oct 13, 2021 · 6 comments
Closed
4 of 5 tasks

Problem with list of objects same keys #207

mebibou opened this issue Oct 13, 2021 · 6 comments
Assignees
Labels

Comments

@mebibou
Copy link
Contributor

mebibou commented Oct 13, 2021

Background Information

  • Module Version: 3.14.4
  • Node/Browser Version: 12.22.6

The issue I'm reporting is with:

  • json2csv
  • csv2json

I have...

  • searched to see if an issue has already been reported.
  • verified that my JSON/CSV data is valid (using something like http://jsonlint.com or https://csvlint.io/).
  • tried upgrading to the latest version of json-2-csv (since the issue may already be fixed).

When doing the following:

json2csv([{
  list: [{
    a: 1
  }, {
    a: 2
  }]
}], {
  expandArrayObjects: true
})

The current resulting csv is:

list.a
[1,2]

which looks like you had instead:

[{
  list: [{
    a: [1, 2]
  }]
}]

But the CSV could actually reflect the original structure if it was like this instead:

list.0.a,list.1.a
1,2

I understand some might want the first result, but it would be nice to have an option to have a more like-to-like representation?

@mrodrig
Copy link
Owner

mrodrig commented Jan 9, 2022

Thanks for reporting this @mebibou. I can definitely see this as being useful as well, especially if converting back to JSON from CSV is a requirement. I'll take a look at how I could make this work with the existing json2csv options.

@JoaoLucasFurtadoa
Copy link

Tank You guys. Congulations!

@whaaaley
Copy link

I just ran into this issue as well. It would be great to have! :D

@sofakingworld
Copy link

Same requirement, it would be useful feature to me

mrodrig added a commit to mrodrig/deeks that referenced this issue Feb 24, 2024
Added to work on support for use case described in mrodrig/json-2-csv#207
@mrodrig mrodrig self-assigned this Feb 25, 2024
mrodrig added a commit that referenced this issue Feb 25, 2024
@mrodrig
Copy link
Owner

mrodrig commented Feb 25, 2024

Thanks to everyone demonstrating support for this use case and issue. I just released version 5.4.0 which updates the deeks and doc-path dependencies to add support for this, while also adding a new arrayIndexesAsKeys option to this package which will allow you to get the desired behavior.

Here's an example:

const { json2csv } = require('json-2-csv');

const data = [
    {
        test: {
            list: [{
                a: 1,
                optionA: 'ac'
            }, {
                a: 2,
                optionB: 'radio'
            }]
        },
    },
    {
        test: {
            list: [{
                a: 3,
                optionA: 'cd'
            }, {
                a: 4,
                optionB: 'heat'
            }]
        }
    }
];
const options = { arrayIndexesAsKeys: true, expandArrayObjects: true };

const csv = json2csv(data, options)
console.log(csv);

which outputs the following CSV:

test.list.0.a,test.list.0.optionA,test.list.1.a,test.list.1.optionB
1,ac,2,radio
3,cd,4,heat

@mrodrig mrodrig closed this as completed Feb 25, 2024
@Previesam
Copy link

Previesam commented Mar 25, 2024

Hi, Thank you for the great work here @mrodrig. Looking at this I can see some issues which may not have been accounted for.

Is this supported in CLI?

Does it work the other way round when converting csv to json? like a csv with key test.list.0 would it be interpreted as an array as well? This does not seem to be the case on the CLI.

I really need to be able to consistently convert from csv to json and reverse without any issue.

So just like this arrays indexes will be used as keys and in converting csv2json the same will be interpreted back to array.

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

No branches or pull requests

6 participants