Skip to content

Instantly share code, notes, and snippets.

@dineshsonachalam
Forked from karmadude/node-mysql2json.js
Created January 15, 2017 20:38
Show Gist options
  • Save dineshsonachalam/677fb4b256b3bf158efaad98c4aa73d9 to your computer and use it in GitHub Desktop.
Save dineshsonachalam/677fb4b256b3bf158efaad98c4aa73d9 to your computer and use it in GitHub Desktop.
Using Node to export MySQL query results to a file as JSON
// https://github.com/felixge/node-mysql
// npm install mysql
var mysql = require('mysql');
// http://nodejs.org/docs/v0.6.5/api/fs.html#fs.writeFile
var fs = require('fs');
var client = mysql.createClient({
user: 'root',
password: 'mysqlpassword'
});
client.query('select * from db.table;', function(err, results, fields) {
if(err) throw err;
fs.writeFile('table.json', JSON.stringify(results), function (err) {
if (err) throw err;
console.log('Saved!');
});
client.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment