Raghu Reddy Raghu Reddy -4 years ago 355
JSON Question

Mysql data need to be written to a json file

I am trying to read data from a mysql database and write it to a JSON file. I am pretty new to node.js and any help would be greatly appreciated.

The code i wrote:

var mysql = require('mysql');
var fs = require('fs');
var configFile = process.cwd() + "\\config.json";
var configFileContents = fs.readFileSync(configFile, 'utf8');
var config = JSON.parse(configFileContents);
//config = config[process.env.BLENV];
console.log(config.PROD.HTTP);

var connection = mysql.createConnection({
host : config.QA.SQLHostName,
user : config.QA.SQLUserName,
password : config.QA.SQLPassword,
database : config.QA.SQLDatabase,
multipleStatements: true
});

connection.connect();

var timeInMs = Date.now();
console.log(timeInMs);

connection.query(`
select
c.name as "Name",
cl.u_geographic_region as "Region",
CASE
WHEN cic.os like '%Windows%' THEN 'Windows'
WHEN cic.os like '%aix%' THEN 'AIX'
WHEN cic.os like '%esx%' THEN 'ESX'
WHEN cic.os like '%linux%' THEN 'Linux'
WHEN cic.os like '%solaris%' THEN 'Solaris'
ELSE 'UNKNOWN'
END as "Operating System",

ci.dns_domain as "Host domain",
ci.ip_address as "IP Address",
CASE
WHEN c.sys_class_name = 'cmdb_ci_computer' THEN 'Computer'
WHEN c.sys_class_name = 'cmdb_ci_win_server' THEN 'Windows Server'
WHEN c.sys_class_name = 'cmdb_ci_esx_server' THEN 'ESX Server'
WHEN c.sys_class_name = 'cmdb_ci_aix_server' THEN 'AIX Server'
WHEN c.sys_class_name = 'cmdb_ci_lb' THEN 'Load Balancer'
WHEN c.sys_class_name = 'cmdb_ci_lb_ace' THEN 'lb_ace'
WHEN c.sys_class_name = 'cmdb_ci_aix_lb_bigip' THEN 'lb_bigip'
WHEN c.sys_class_name = 'cmdb_ci_linux_server' THEN 'Linux Server'
WHEN c.sys_class_name = 'cmdb_ci_mainframe' THEN 'IBM Mainframe'
WHEN c.sys_class_name = 'cmdb_ci_solaris_server' THEN 'Solaris Server'
WHEN c.sys_class_name = 'cmdb_u_ci_ibmi_server' THEN 'IBMi Server'
WHEN c.sys_class_name = 'cmdb_ u_ci_ibmi_server' THEN 'IBMi Server'
WHEN c.sys_class_name = 'cmdb_u_server_appliance' THEN 'ServerAppliance'
ELSE 'Unknown'
END as "Class",

CASE
WHEN c.install_status = '1' THEN 'Installed'
WHEN c.install_status = '100' THEN 'Absent'
WHEN c.install_status = '2' THEN 'On Order'
WHEN c.install_status = '3' THEN 'In Maintenance'
WHEN c.install_status = '4' THEN 'Pending Install'
WHEN c.install_status = '5' THEN 'Pending Repair'
WHEN c.install_status = '6' THEN 'In Stock'
WHEN c.install_status = '7' THEN 'Retired'
WHEN c.install_status = '8' THEN 'Stolen'
WHEN c.install_status = '9' THEN 'In Use'
ELSE 'Unknown'
END As "lifecycle Status"

from
cmdb as c
join cmdb_ci ci on c.sys_id = ci.sys_id
join cmdb_ci_hardware as cih on c.sys_id = cih.sys_id
join cmdb_ci_computer cic on c.sys_id = cic.sys_id
join cmn_location as cl on cl.sys_id = c.location
where c.name is not null
`, function(err, rows, fields) {
if (err) throw err;

for (var i in rows) {
console.log('the rows are', rows[i]);
}
});

connection.end();


Now when am running the code using "node filename.js" am able to print the output to my console. But i want it to be written to a json file so that i can process the data and apply some logic.

Answer Source

If all you want to do is write the output of the MySQL data in JSON to a file, replace...

for (var i in rows) {
 console.log('the rows are', rows[i]);
}

...with:

fs.writeFile("/path/to/file.json", JSON.stringify(rows), function(err) {
    if (err) return console.log(err);    
    console.log("The file was saved!");
});

More about JSON.stringify() at https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download