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

Complex JSON array - unexpected behavior #78

Open
iuabhmalat opened this issue Jul 7, 2020 · 1 comment · May be fixed by #81
Open

Complex JSON array - unexpected behavior #78

iuabhmalat opened this issue Jul 7, 2020 · 1 comment · May be fixed by #81

Comments

@iuabhmalat
Copy link

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

iuabhmalat added a commit to iuabhmalat/jsonexport that referenced this issue Jul 23, 2020
…sparse so that fillGaps finds the columns for the first row accurately. Updated test to use os.EOL instead of \n for object.js
@iuabhmalat iuabhmalat linked a pull request Jul 23, 2020 that will close this issue
1 task
@AckerApple
Copy link
Collaborator

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.

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

Successfully merging a pull request may close this issue.

2 participants