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

This module reverses the order of json key values #7

Closed kzimic closed 8 years ago

kzimic commented 8 years ago
// JSON
[ { id: 84192,
    parent_id: 3551,
    product_parent_id: null,
    regular: 1,
    parent: 0,
    child: 0,
    configuration: '(none)',
    name: 'name',
    alias: 'alias',
    part_number: 'PD-LPM-0102EP',
    description: null,
    status: 'Obsolete',
    published: 1,
    pb_free: 0,
    rohs: 0,
    hirel: 0,
    is_new: 0,
    registerable: 1,
    navigation: 1,
    search: 1 },
  { id: 84193,
    parent_id: 3551,
    product_parent_id: null,
    regular: 1,
    parent: 0,
    child: 0,
    configuration: '(none)',
    name: 'name',
    alias: 'alias',
    part_number: 'PD-NPM-0305EP',
    description: null,
    status: 'Obsolete',
    published: 1,
    pb_free: 0,
    rohs: 0,
    hirel: 0,
    is_new: 0,
    registerable: 1,
    navigation: 1,
    search: 1 },
  { id: 84194,
    parent_id: 3551,
    product_parent_id: null,
    regular: 1,
    parent: 0,
    child: 0,
    configuration: '(none)',
    name: 'name',
    alias: 'alias',
    part_number: 'PD-NPM-0306EP',
    description: null,
    status: 'Obsolete',
    published: 1,
    pb_free: 0,
    rohs: 0,
    hirel: 0,
    is_new: 0,
    registerable: 1,
    navigation: 1,
    search: 1 },
  { id: 84944,
    parent_id: 3551,
    product_parent_id: null,
    regular: 1,
    parent: 0,
    child: 0,
    configuration: '(none)',
    name: 'name',
    alias: 'alias',
    part_number: 'PD70101ILQ',
    description: null,
    status: 'In Production',
    published: 1,
    pb_free: 1,
    rohs: 1,
    hirel: 0,
    is_new: 0,
    registerable: 1,
    navigation: 1,
    search: 1 },
  { id: 84945,
    parent_id: 3551,
    product_parent_id: null,
    regular: 1,
    parent: 0,
    child: 0,
    configuration: '(none)',
    name: 'name',
    alias: 'alias',
    part_number: 'PD70201ILQ',
    description: null,
    status: 'In Production',
    published: 1,
    pb_free: 1,
    rohs: 1,
    hirel: 0,
    is_new: 0,
    registerable: 1,
    navigation: 1,
    search: 1 },
  { id: 84195,
    parent_id: 3551,
    product_parent_id: null,
    regular: 1,
    parent: 0,
    child: 0,
    configuration: '(none)',
    name: 'name',
    alias: 'alias',
    part_number: 'PD-NPM-0307EP',
    description: null,
    status: 'Obsolete',
    published: 1,
    pb_free: 0,
    rohs: 0,
    hirel: 0,
    is_new: 0,
    registerable: 1,
    navigation: 1,
    search: 1 },
  { id: 84196,
    parent_id: 3551,
    product_parent_id: null,
    regular: 1,
    parent: 0,
    child: 0,
    configuration: '(none)',
    name: 'name',
    alias: 'alias',
    part_number: 'PD-NPM-0308EP',
    description: null,
    status: 'Obsolete',
    published: 1,
    pb_free: 0,
    rohs: 0,
    hirel: 0,
    is_new: 0,
    registerable: 1,
    navigation: 1,
    search: 1 },
  { id: 136582,
    parent_id: 3551,
    product_parent_id: null,
    regular: 1,
    parent: 0,
    child: 0,
    configuration: '0',
    name: 'name',
    alias: 'alias',
    part_number: 'PD81001',
    description: null,
    status: 'In Production',
    published: 1,
    pb_free: 1,
    rohs: 1,
    hirel: 0,
    is_new: 0,
    registerable: 1,
    navigation: 1,
    search: 1 } ]

//CSV
search,navigation,registerable,is_new,hirel,rohs,pb_free,published,status,part_number,alias,name,configuration,child,parent,regular,parent_id,id
1,1,1,0,0,0,0,1,Obsolete,PD-LPM-0102EP,alias,name,(none),0,0,1,3551,84192
1,1,1,0,0,0,0,1,Obsolete,PD-NPM-0305EP,alias,name,(none),0,0,1,3551,84193
1,1,1,0,0,0,0,1,Obsolete,PD-NPM-0306EP,alias,name,(none),0,0,1,3551,84194
1,1,1,0,0,1,1,1,In Production,PD70101ILQ,alias,name,(none),0,0,1,3551,84944
1,1,1,0,0,1,1,1,In Production,PD70201ILQ,alias,name,(none),0,0,1,3551,84945
1,1,1,0,0,0,0,1,Obsolete,PD-NPM-0307EP,alias,name,(none),0,0,1,3551,84195
1,1,1,0,0,0,0,1,Obsolete,PD-NPM-0308EP,alias,name,(none),0,0,1,3551,84196
1,1,1,0,0,1,1,1,In Production,PD81001,alias,name,0,0,0,1,3551,136582
kaue commented 8 years ago

Try the option orderHeaders: false

kzimic commented 8 years ago

That solved the problem, Thanks!

var jsonexport = require('jsonexport');

var options = {
    orderHeaders: false
};

router.get('/:catid', function (req, res, next) {

    var catData;

    models.Product.findAll({

        where: {parent_id: req.params.catid},
        include: [
            {
                model: models.ProductNumeric
            },
            {
                model: models.ProductAlphanumeric
            }
        ]

    }).then(function (category) {
        var dt = datetime.create();
        var currentDate = dt.format('Y/m/d');
        jsonexport(JSON.parse(JSON.stringify(category)),options,function(err, csv){
            if(err) return console.log(err);
            res.setHeader('Content-disposition', 'attachment; filename=category_' + req.params.catid + '_' + currentDate + '.csv');
            res.setHeader('Content-type', 'text/csv');
            res.charset = 'UTF-8';
            res.write(csv);
            res.end();
        });

    });
});

//CSV

id,parent_id,regular,parent,child,configuration,name,alias,part_number,status,published,pb_free,rohs,hirel,is_new,registerable,navigation,search
84192,3551,1,0,0,(none),name,alias,PD-LPM-0102EP,Obsolete,1,0,0,0,0,1,1,1
84193,3551,1,0,0,(none),name,alias,PD-NPM-0305EP,Obsolete,1,0,0,0,0,1,1,1
84194,3551,1,0,0,(none),name,alias,PD-NPM-0306EP,Obsolete,1,0,0,0,0,1,1,1
84944,3551,1,0,0,(none),name,alias,PD70101ILQ,In Production,1,1,1,0,0,1,1,1
84945,3551,1,0,0,(none),name,alias,PD70201ILQ,In Production,1,1,1,0,0,1,1,1
84195,3551,1,0,0,(none),name,alias,PD-NPM-0307EP,Obsolete,1,0,0,0,0,1,1,1
84196,3551,1,0,0,(none),name,alias,PD-NPM-0308EP,Obsolete,1,0,0,0,0,1,1,1
136582,3551,1,0,0,0,name,alias,PD81001,In Production,1,1,1,0,0,1,1,1