Skip to content
Advertisement

Converting sql data to json but json file include recordset

I’m trying to convert sql data to json data but when file is completed I get something ‘recordset’ what is this and how do i get rid of it.

  app.post("/Employees",function (req, res)  {
           const sql = require("mssql");
    
           const config = {
          
            server: 'HOST',  //update me
            user: 'user', //update me
            password: 'pass',  //update me
            database: 'db',
            trustServerCertificate: true
           }
    
           // var to store file name 
           var d = new Date();
             var fileName = "db_"+d.getDate()+"_"+d.getMonth()+"_"+d.getTime();
           sql.connect(config, function(err, data) {
            
             if(err) console.log(err);
               let sqlRequest = new sql.Request();
    
               let sqlQuery = "select * from dbo.cust where name like 'aa%' AND surname like 'a%' AND branch='bsmtr';";
               sqlRequest.query(sqlQuery, function (err, data) {
                 if(err) console.log(err) 
                 console.log(data);
               
                 fs.writeFile(`json_files/${fileName}.json`, JSON.stringify(data, replacer, space), err => err && console.log(err));
                 sql.close();
               });
           });
          
           res.send("The JSON file is downloaded please find it in the Json directory by the name "+fileName+".JSON");
        });

—–This is how I am getting the json .—

{
    "recordsets": [
        [
            {
                "rowno": 294680503,
                "rowno_custcrm_crm": 0,
                "has_custansp_ansp": 0,
                "rowno_custemt_emt": 0,
                "rowno_custmtm_mtm": 0,
                "rowno_custm2mc_m2mcu": 0,
                "has_custext_ext": 0,
                "rowno_custcloa_cloa": 0,
                "has_custalrt_alrt": 0,
                "rowno_custemcc_emcc": 0,
                "rowno_custiop_iop": 0,
                "has_custcds_cds": 0,
                "has_custltv_ltv": 0,
                "rowno_custoir_oir": 0,
                "rowno_custorcc_orcc": 0,
                "rowno_custcmps_cmps": 0
            }
        ]
    ],
   
    "output": {},
    "rowsAffected": 1
}

Please tell me how i get rid of recordset, square brackets and output and rowsafftectd

Advertisement

Answer

Well, you just need to stringify the data you actually want to put into a file. e.g. if you want the content of the recordset you need to call JSON.stringify(data.recordset, replacer, space). If you want to put that one element that you have in the recordset you need to use JSON.stringify(data.recordsets[0][0], replacer, space)

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement