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

Wrong placement in multi-array set #22

Closed
decaller opened this issue May 3, 2017 · 9 comments · Fixed by #68
Closed

Wrong placement in multi-array set #22

decaller opened this issue May 3, 2017 · 9 comments · Fixed by #68
Labels
enhancement ready to close Issue reporter will close when satisfied
Milestone

Comments

@decaller
Copy link

decaller commented May 3, 2017

Here's my code

var test = [
    {
      "name" : "John",
      "clases" : [
        {
          "year" : 2016,
          "grade" : "1st",
          "payment":[
            {
              "type":"monthly",
              "status":"done"
            },
            {
              "type":"yearly",
              "status":"done"
            }
          ]
        },
        {
          "year" : 2017,
          "grade" : "2nd",
          "payment":[
            {
              "type":"monthly",
              "status":"in progress"
            },
            {
              "type":"yearly",
              "status":"done"
            }
          ]
        }
      ],
      
    },
    {
      "name" : "Andrew",
      "clases" : [
        {
          "year" : 2017,
          "grade" : "1nd",
          "payment":[
            {
              "type":"monthly",
              "status":"in progress"
            },
            {
              "type":"yearly",
              "status":"in progress"
            }
          ]
        }
      ]
    },
    
  ]

jsonexport(test,function(err, csv){
    if(err) return console.log(err);
    console.log(csv);
});

and here is the output

name,clases.year,clases.grade,clases.payment.type,clases.payment.status
John,2016,1st,monthly,done
,2017,2nd,yearly,done
,,,monthly,in progress
,,,yearly,done
Andrew,2017,1nd,monthly,in progress
,,,yearly,in progress

capture

as you can see, the values are not supposed to be there. It should drop on the next line.
And is there any option to force rewrite values so it would be look like this?

capture

Thank you..

@kaue
Copy link
Owner

kaue commented Jun 6, 2017

@decaller Should be fixed with 7270f48

@kaue kaue mentioned this issue Jun 6, 2017
@kaue
Copy link
Owner

kaue commented Jun 6, 2017

@decaller In v2.0.0 you can use fillGaps option to get this behavior.

@kaue kaue closed this as completed Jun 6, 2017
@decaller
Copy link
Author

decaller commented Jun 8, 2017

thank you for the update, really appreciate it

@saipallavi
Copy link

saipallavi commented Jun 15, 2017

Hi,
I use similar kind of multi-array set. I am using the below input:

var test=[{
    "CODE": "AA",
    "GROUP_NAME": "aaaaaa",
    "GROUP_DESC": "aaaaaaaaa",
    "CHILD_GROUP": [{
            "CODE": "AA",
            "GROUP_NAME": "aaaaaa",
            "CHILD_CODE": "bb"
        },
        {
            "CODE": "AA",
            "GROUP_NAME": "aaaaaa",
            "CHILD_CODE": "cc"
        },
        {
            "CODE": "AA",
            "GROUP_NAME": "aaaaaa",
            "CHILD_CODE": "dd"
        }
    ]
}]
 jsonexport(test,function(err, csv){
    if(err) return console.log(err);
    console.log(csv);
});

And my output is :

CODE,GROUP_NAME,GROUP_DESC,CHILD_GROUP.CODE,CHILD_GROUP.GROUP_NAME,CHILD_GROUP.CHILD_CODE
AA,aaaaaa,aaaaaaaaa,AA,aaaaaa,bb
,,,AA,aaaaaa,cc
,,,AA,aaaaaa,dd

The first child is getting appended to the parent. Could you please provide a solution for this???

I need the output like this:
AA,aaaaaa,aaaaaaaaa
,,,AA,aaaaaa,bb
,,,AA,aaaaaa,cc
,,,AA,aaaaaa,dd

@kaue kaue reopened this Jun 15, 2017
Repository owner deleted a comment from saipallavi Jun 29, 2017
@AckerApple
Copy link
Collaborator

I abolsutely think this issue should be closed.

@saipallavi, I have starred your example over for quite sometime. I disagree with you, I see expected result. A child-arrary's first row belongs with-in the parent row. If jsonexport did what you are asking for, it'd be a hard breaking change. Your "I need the output like this" example, would actually mean you have a 2nd parent who's CODE, GROUP_NAME, and GROUP_DESC are all null but has a 3 child length array (not to mention row 1 has no children).

Lets close this. Wouldn't even offer an option to pivot the behavior to keep tight format standards

@kaue
Copy link
Owner

kaue commented Sep 2, 2017

@AckerApple I think there is a way we can provide this behavior without breaking changes.
If we add a new option handleArray(array) that allows the user to change the array like the other handle functions.

To do this we could add this call

array = self._options.handleArray(array);

At: https://github.com/kauegimenes/jsonexport/blob/master/lib/parser/handler.js#L100

Usage example

var jsonexport = require('jsonexport');

var data = [{
    "CODE": "AA",
    "GROUP_NAME": "aaaaaa",
    "GROUP_DESC": "aaaaaaaaa",
    "CHILD_GROUP": [{
            "CODE": "AA",
            "GROUP_NAME": "aaaaaa",
            "CHILD_CODE": "bb"
        },
        {
            "CODE": "AA",
            "GROUP_NAME": "aaaaaa",
            "CHILD_CODE": "cc"
        },
        {
            "CODE": "AA",
            "GROUP_NAME": "aaaaaa",
            "CHILD_CODE": "dd"
        }
    ]
}];

var options = {
    handleArray: function(array){
        return [{CODE: " ", GROUP_NAME: " ", CHILD_CODE: " "}].concat(array);
    }
};

jsonexport(data, options, function(err, csv){
    if(err) return console.log(err);
    console.log(csv);
});

What you think about this @AckerApple and @saipallavi ?

@AckerApple
Copy link
Collaborator

My thought is that it will get awfully ugly and convoluted for arrays inside arrays, or array of objects with arrays.

I wouldn't approach this one as a catering company. Don't do, a change like this needs an accompanying document type. CSV doesn't have metadata by nature. You best stick to as little format variations as possible.

We can do anything, don't think we should for consistency sake

@kaue
Copy link
Owner

kaue commented May 23, 2020

@saipallavi should be fixed in 3.0.0 after #68 gets merged

@kaue kaue added the ready to close Issue reporter will close when satisfied label May 23, 2020
@kaue kaue added this to the 3.0.0 milestone May 23, 2020
@kaue kaue linked a pull request May 23, 2020 that will close this issue
@kaue kaue mentioned this issue May 23, 2020
3 tasks
@kaue
Copy link
Owner

kaue commented May 30, 2020

@saipallavi jsonexport@3.0.0 got released, feel free to reopen this issue if you still get the incorrect output.

@kaue kaue closed this as completed May 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement ready to close Issue reporter will close when satisfied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants