Skip to content

Empty Array Being Returned as Header causing JSON to be Outputted in CSV #168

Open
@hdwatts

Description

@hdwatts

Background Information

  • Module Version: 3.7.8
  • Node/Browser Version: 86.0.4240.80

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

entities.id,entities.name,entities.siblings.entity.name,entities.siblings.relationship_set.relationships.relationship.name
4,Zeus,Exo,Senior
4,Zeus,Chrono,null

Actual Behavior

entities.id,entities.name,entities.siblings.entity.name,entities.siblings.relationship_set.relationships.relationship.name,entities.siblings.relationship_set.relationships
4,Zeus,Exo,Senior,"{""relationship"":{""name"":""Senior""}}"
4,Zeus,Chrono,null,

Data Sample

JSON:

{
  "entities":
    {
      "id": 4,
      "name": "Zeus",
      "siblings": [
        {
          "entity": {
            "name": "Exo",
          },
          "relationship_set": {
            "relationships": [
              {
                "relationship": {
                  "name": "Senior"
                },
              },
            ]
          }
        },
        {
          "entity": {
            "name": "Chrono"
          },
          "relationship_set": {
            "relationships": []
          }
        }
      ]
    }
}

Code Example

// Please include a simple example to replicate the issue
let converter = require('json-2-csv');
const obj1 = {
  "entities":
    {
      "id": 4,
      "name": "Zeus",
      "siblings": [
        {
          "entity": {
            "name": "Exo",
          },
          "relationship_set": {
            "relationships": [
              {
                "relationship": {
                  "name": "Senior"
                },
              },
            ]
          }
        },
        {
          "entity": {
            "name": "Chrono"
          },
          "relationship_set": {
            "relationships": []
          }
        }
      ]
    }
}

converter.json2csv(obj1, (err, csv) => {
    console.log(csv)
  }, { unwindArrays: true, expandArrayObjects: true })

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions