kaue / jsonexport

{} → :page_facing_up: it's easy to convert JSON to CSV
http://kaue.github.io/jsonexport/
Apache License 2.0
247 stars 41 forks source link

Wrong placement in multi-array set #22

Closed decaller closed 4 years ago

decaller commented 7 years ago

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 commented 7 years ago

@decaller Should be fixed with https://github.com/kauegimenes/jsonexport/commit/7270f4873ba8e7fa87027ce52546c08122d7aaef

kaue commented 7 years ago

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

decaller commented 7 years ago

thank you for the update, really appreciate it

saipallavi commented 7 years ago

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

AckerApple commented 7 years ago

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 commented 7 years ago

@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 commented 7 years ago

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 commented 4 years ago

@saipallavi should be fixed in 3.0.0 after https://github.com/kauegimenes/jsonexport/pull/68 gets merged

kaue commented 4 years ago

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