Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert select query output to Json format in NodeJS

Tags:

json

node.js

db2

I am doing a select query on a DB2 database using NodeJS. The query returns FIRST 5 ROWS.

I want to convert these 5 statement that the query returns into a Json format object. Is there something that is already built that i can reuse to convert query to json ?

var ibmdb = require('ibm_db');

ibmdb.open("DRIVER={DB2};DATABASE=profiledb;HOSTNAME=158.xx.xx.xxx;UID=user;PWD=pass;PORT=1234;PROTOCOL=TCPIP", function (err,conn) {
  if (err) return console.log(err);

  conn.query('SELECT STMT_TEXT, ESTIMATE FROM TABLEUSUAGE WHERE TIMESTAMP > (SELECT CURRENT DATE - 2 DAY FROM SYS.SYSDUMMY1) ORDER BY ESTIMATE DESC FETCH FIRST 5 ROWS ONLY', function (err, data) {
    if (err) console.log(err);

    var data_from_query = data; 

    console.log(data);

    conn.close(function () {
      console.log('done');
    });
  });

});
like image 562
user3846091 Avatar asked Dec 15 '22 18:12

user3846091


1 Answers

Yes try the below statement that would convert your output to Json format.

 console.log(JSON.stringify(data));
like image 197
Marsh Avatar answered Jan 13 '23 11:01

Marsh