Amit Adhikari Amit Adhikari - 3 months ago 10
JSON Question

Nested arrays in object json to csv

Im using 'json-csv' library to create a csv from a users arrays with nested objects and arrays.

var users = [
{
subscriptions: [
{
package : {
name: 'Grammar'
},
state: 'EXPIRED',
timerange: {
period : 5550
},
transaction:{
amount: 10000
}
},
{
package : {
name: 'GK'
},
state: 'ACTIVE',
timerange: {
period : 30
},
transaction:{
amount: 10340
}
},
],
account:{
balance: 200
},
name: "Johhy Moe",
email: null,
user_id: "123456789",
username: null,
user_type: "facebook",
id: 3,
createdAt: "2016-07-11T08:02:40.000Z",
updatedAt: "2016-07-11T08:02:40.000Z",

},
{
subscriptions: [
{
package : {
name: 'GK'
},
state: 'EXPIRED',
timerange: {
period : 42
},
transaction:{
amount: 5252
}
},
{
package : {
name: 'MATH'
},
state: 'ACTIVE',
timerange: {
period : 25
},
transaction:{
amount: 200
}
}
],
account:{
balance: 1500
},
name: "John Doe",
email: null,
user_id: "123456789",
username: null,
user_type: "facebook",
id: 7,
createdAt: "2016-07-29T06:44:18.000Z",
updatedAt: "2016-07-29T06:44:18.000Z"
},
]


Now i want the generated csv to be like this
USERID,NAME,FBID,ACCOUNT,SUBSCRIPTION,PRICE,STATE,TIMEPERIOD

3,Johhy Moe,123456789,200,Grammar,10000,EXPIRED,5550

3,Johhy Moe,123456789,200,GK,10340,ACTIVE,30

7,John Doe,123456789,1500,GK,5252,EXPIRED,30

7,John Doe,123456789,1500,MATH,200,ACTIVE,25

As you see if there are two objects inside subscription array for each user, i want to repeat that user again but with different subscription data.

I've thought of using the library because my users array can go up to thousands of users with hundreds of subscription.

And i'm at a loss to what i should do.
my Code:

var options= {
fields : [
{
name : 'id',
label : 'USERID'
},
{
name : 'name',
label : 'Name'
},
{
name : 'user_id',
label : 'FBID'
},
{
name : 'account.balance',
label : 'ACCOUNT'
},
{
name: '',
label: 'Subscription'
}
]
}
var source = es.readArray(users)
source
.pipe(jsoncsv.csv(options))
.pipe(res)


I dont want to use a library also. So if someone could provide me with a resource to make my own csv file with strings and also using streams , that would be great. Thanks!!

Answer

This will solve your problem. Now you just have to change console.log to fs and write to your file.

var json2csv = function (json, listKeys) {
    var str    = "";
    var prefix = "";
    for (var i = 0; i < listKeys.length; i++) {
        str += prefix + json[listKeys[i]];
        prefix = ",";
    }
    return str;
};

var async = require('async');
var csvData = ['USERID,NAME,FBID,ACCOUNT,SUBSCRIPTION,PRICE,STATE,TIMEPERIOD'];
async.each(users, function (user, callback) {
    var csvRow1 = {
        USERID:  user.id,
        NAME:    user.name,
        FBID:    user.user_id,
        ACCOUNT: user.account.balance
    };
    async.each(user.subscriptions, function (subscription, callback) {
        var csvRow2          = JSON.parse(JSON.stringify(csvRow1));
        csvRow2.SUBSCRIPTION = subscription.package.name;
        csvRow2.PRICE        = subscription.transaction.amount;
        csvRow2.STATE        = subscription.state;
        csvRow2.TIMEPERIOD   = subscription.timerange.period;
        csvData.push(json2csv(csvRow2, ['USERID', 'NAME', 'FBID', 'ACCOUNT', 'SUBSCRIPTION', 'PRICE', 'STATE', 'TIMEPERIOD']));
        callback(null);
    }, function (err) {
        callback(err);
    });
}, function (err) {
    if (err) {
        // return err;
    } else {
        // return csvData;
    }
});
Comments