Skip to content

[bug]: Nested arrays are not unwound from 3+ levels deep #273

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

Closed
4 of 5 tasks
jose-cabral opened this issue Mar 4, 2025 · 1 comment
Closed
4 of 5 tasks

[bug]: Nested arrays are not unwound from 3+ levels deep #273

jose-cabral opened this issue Mar 4, 2025 · 1 comment
Labels
bug json2csv JSON to CSV
Milestone

Comments

@jose-cabral
Copy link
Contributor

jose-cabral commented Mar 4, 2025

Background Information

  • Module Version: 5.5.8
  • Node/Browser Version: 20.0.9

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).

Expected Behavior

When using expandArrayObjects: true and unwindArrays: true, all existent arrays should be expanded and unwonded.

Actual Behavior

From 3+ levels deep it does not always unwind or even expand all the arrays.


Example 1 ✅

Data Sample

JSON:

[
    {
      "Countries": [
        {
          "Cities": [
            {
              "Streets": [
                {
                  "Name": "Road 1",
                  "Number": 1
                },
                {
                  "Name": "Road 2",
                  "Number": 2
                }
              ]
            },
            {
              "field": "value"
            }
          ]
        }
      ]
    }
  ]

Expected CSV

Countries.Cities.Streets.Name Countries.Cities.Streets.Number Countries.Cities.field
Road 1 1 ---
Road 2 2 ---
--- --- value

Actual CSV✅

Countries.Cities.Streets.Name Countries.Cities.Streets.Number Countries.Cities.field
Road 1 1 ---
Road 2 2 ---
--- --- value

Code Example

// index.mjs
import { json2csv } from 'json-2-csv';

const options = {
    expandArrayObjects: true,
    unwindArrays: true,
    emptyFieldValue: '---' // just for the sake of readability
};
const json = [{"Countries":[{"Cities":[{"Streets":[{"Name":"Road 1","Number":1},{"Name":"Road 2","Number":2}]},{"field":"value"}]}]}];
const csv = json2csv(json, options);

console.log(csv);

Example 2 ❌

Data Sample

JSON:

[
    {
      "Countries": [
        {
          "Cities": [
            {
              "Streets": [
                {
                  "Name": "Road 1",
                  "Number": 1
                },
                {
                  "Name": "Road 2",
                  "Number": 2
                }
              ]
            }
          ]
        }
      ]
    }
  ]

Expected CSV

Countries.Cities.Streets.Name Countries.Cities.Streets.Number
Road 1 1
Road 2 2

Actual CSV❌

Countries.Cities.Streets
[{"Name":"Road 1","Number":1},{"Name":"Road 2","Number":2}]

Code Example

// index.mjs
import { json2csv } from 'json-2-csv';

const options = {
    expandArrayObjects: true,
    unwindArrays: true,
    emptyFieldValue: '---' // just for the sake of readability
};
const json = [{"Countries":[{"Cities":[{"Streets":[{"Name":"Road 1","Number":1},{"Name":"Road 2","Number":2}]}]}]}];
const csv = json2csv(json, options);

console.log(csv);

Example 3 ❌

Data Sample

JSON:

[
    {
      "Continents": [
        {
          "Countries": [
            {
              "Cities": [
                {
                  "Streets": [
                    {
                      "Name": "Road 1",
                      "Number": 1
                    },
                    {
                      "Name": "Road 2",
                      "Number": 2
                    }
                  ]
                },
                {
                  "field": "value"
                }
              ]
            }
          ]
        }
      ]
    }
  ]

Expected CSV

Continents.Countries.Cities.Streets.Name Continents.Countries.Cities.Streets.Number Continents.Countries.Cities.field
Road 1 1 ---
Road 2 2 ---
--- --- value

Actual CSV❌

Continents.Countries.Cities
[{"Streets":[{"Name":"Road 1","Number":1},{"Name":"Road 2","Number":2}]},{"field":"value"}]

Code Example

// index.mjs
import { json2csv } from 'json-2-csv';

const options = {
    expandArrayObjects: true,
    unwindArrays: true,
    emptyFieldValue: '---' // just for the sake of readability
};
const json = [{"Continents":[{"Countries":[{"Cities":[{"Streets":[{"Name":"Road 1","Number":1},{"Name":"Road 2","Number":2}]},{"field":"value"}]}]}]}];
const csv = json2csv(json, options);

console.log(csv);

Note

I have the fix for this, I will open a PR soon

@jose-cabral jose-cabral changed the title [bug]: Nested arrays are not unwond from 3+ levels deep [bug]: Nested arrays are not unwound from 3+ levels deep Mar 4, 2025
jose-cabral pushed a commit to jose-cabral/json-2-csv that referenced this issue Mar 4, 2025
jose-cabral pushed a commit to jose-cabral/json-2-csv that referenced this issue Mar 4, 2025
jose-cabral pushed a commit to jose-cabral/json-2-csv that referenced this issue Mar 4, 2025
jose-cabral pushed a commit to jose-cabral/json-2-csv that referenced this issue Mar 4, 2025
jose-cabral added a commit to jose-cabral/json-2-csv that referenced this issue Mar 4, 2025
@jose-cabral
Copy link
Contributor Author

PR is open

@mrodrig mrodrig closed this as completed in 32ca8c4 Mar 6, 2025
mrodrig added a commit that referenced this issue Mar 6, 2025
fixes #273: nested arrays are not always unwound
@mrodrig mrodrig added bug json2csv JSON to CSV labels Mar 6, 2025
@mrodrig mrodrig added this to the 5.5.9 milestone Mar 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug json2csv JSON to CSV
Projects
None yet
Development

No branches or pull requests

2 participants