I am trying to push key, value in array and then converting it into JSON using JSON.stringify(). But it is not working.
My node.js code:
var jarray=[];
var json1="";
for (var i=0; i<jsonObj["Masters"]['Customer'].length; i++){
var name= jsonObj["Masters"]['Customer'][i];
var cust_name=name['Customer_Name'];
var cust_code=name['Customer_Code'];
connection.query("SELECT code FROM ((SELECT ccode AS code FROM customermaster WHERE companyid='AXWPM1658D') UNION ALL (SELECT scode AS code FROM suppliermaster WHERE companyid='AXWPM1658D') UNION ALL (SELECT stcode AS code FROM stockmaster WHERE companyid='AXWPM1658D') UNION ALL (SELECT gcode AS code FROM generalledger2 WHERE companyid='AXWPM1658D') UNION ALL (SELECT bcode AS code FROM bankmaster WHERE companyid='AXWPM1658D'))p where code='"+cust_code+"' ", function(err, rows, fields) {
if (!err){
var item ={"customer_name":cust_name ,"customer_code": cust_code };
jarray.push(item);
}
else{
console.log('Error while performing Query.'+err);
}
});
}
json1=JSON.stringify({jarray:jarray});
var jsonObj1 = JSON.parse(json1);
console.log("Json:"+jsonObj1);
console.log("arr length:"+jsonObj1.jarray.length);
It prints:
Json:{ jarray: [] }
arr length:0
My question is how to push values in array and convert it into JSON array?