kaue / jsonexport

{} → :page_facing_up: it's easy to convert JSON to CSV

Home Page:http://kaue.github.io/jsonexport/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Complex JSON array - unexpected behavior

iuabhmalat opened this issue · comments

Hello,
I am using jsonexport library to convert a complex JSON object into csv, and running into some unexpected behavior. Here's my code-


var jsonexport = require('jsonexport');

var contacts = [
  {
    "id": "101",
    "resourceType": "Employee",
    "meta": {
      "profile": ["1q2w3e4r"],
      "tag": [
        {
          "system": "salary",
          "code": "2w1e3r34t"
        }, 
        {
          "system": "pto",
          "code": "33errfee"
        }, 
        {
          "system": "benefits",
          "code": "jiojofirf494r93"
        }
      ]
    },
    "category": [
      {
        "coding": [
          {
            "system": "orgchart",
            "org": "sales",
            "display": "manager"
          }
        ]
      }
    ],
	"benefits": {
      "coding": [
        {
          "system": "ePTO",
          "code": "387ry329r82y3",
          "display": "6 weeks paid vacation"
        }, 
        {
          "system": "insurance",
          "code": "754689",
          "display": "Comprehensive"
        }
      ]
    }
  },
  {
    "id": "102",
    "resourceType": "Employee",
    "meta": {
      "profile": ["e3r3434r"],
      "tag": [
        {
          "system": "salary",
          "code": "t65y5y"
        }, 
        {
          "system": "pto",
          "code": "er3r32r23r2"
        }, 
        {
          "system": "benefits",
          "code": "fwf3232"
        }
      ]
    },
    "category": [
      {
        "coding": [
          {
            "system": "orgchart",
            "org": "sales",
            "display": "engineer"
          }
        ]
      }
    ],
	"benefits": {
      "coding": [
        {
          "system": "ePTO",
          "code": "2w3e323",
          "display": "2 weeks paid vacation"
        }, 
        {
          "system": "insurance",
          "code": "t43t43",
          "display": "Bare minimum"
        }
      ]
    }
  }
];

jsonexport(contacts, {
      forceTextDelimiter: true,
      fillGaps: true,
      fillTopRow: true
    },function(err, csv){
    if(err) return console.log(err);
    console.log(csv);
});

The actual output is -

"id","resourceType","meta.profile","meta.tag.system","meta.tag.code","category.coding.system","category.coding.org","category.coding.display","benefits.coding.system","benefits.coding.code","benefits.coding.display"
"101","Employee","1q2w3e4r","salary","2w1e3r34t","orgchart","sales","manager","ePTO","387ry329r82y3","6 weeks paid vacation"
"101","Employee","1q2w3e4r","pto","33errfee",,,,"insurance","754689","Comprehensive"
"101","Employee","1q2w3e4r","benefits","jiojofirf494r93",,,,"insurance","754689","Comprehensive"
"102","Employee","e3r3434r","salary","t65y5y","orgchart","sales","engineer","ePTO","2w3e323","2 weeks paid vacation"
"102","Employee","e3r3434r","pto","er3r32r23r2","orgchart","sales","engineer","insurance","t43t43","Bare minimum"
"102","Employee","e3r3434r","benefits","fwf3232","orgchart","sales","engineer","insurance","t43t43","Bare minimum"

(Pasting tabular format for visibility):

id resourceType meta.profile meta.tag.system meta.tag.code category.coding.system category.coding.org category.coding.display benefits.coding.system benefits.coding.code benefits.coding.display
101 Employee 1q2w3e4r salary 2w1e3r34t orgchart sales manager ePTO 387ry329r82y3 6 weeks paid vacation
101 Employee 1q2w3e4r pto 33errfee insurance 754689 Comprehensive
101 Employee 1q2w3e4r benefits jiojofirf494r93 insurance 754689 Comprehensive
102 Employee e3r3434r salary t65y5y orgchart sales engineer ePTO 2w3e323 2 weeks paid vacation
102 Employee e3r3434r pto er3r32r23r2 orgchart sales engineer insurance t43t43 Bare minimum
102 Employee e3r3434r benefits fwf3232 orgchart sales engineer insurance t43t43 Bare minimum

jsonexport fails to fill the 2nd and 3rd row for category.coding fields for the first record (see empty cells in rows 2 and 3), but does it for the second. Interestingly, it fills the last object of benefits.coding on the third row for each record. Do you know why jsonexport treats first record fields differently than the second? The JSON structure is identical for both records.

Abhi

We’ve had similar callouts with keywords of “complex array”. However the details here are too large scale for someone donating there time to pick up and run with. I wish the provided details and example were reduce to a small easy to understand scope.