Search code examples
node.jsjsonoracle-databaseclob

Divide Oracle CLOB to Multiple JSON with Node JS


I am trying to convert a Single Oracle CLOB data into Multiple JSON file with Node js. I have used oracledb.fetchAsString = [ oracledb.CLOB ]; to fetch Oracle CLOB data. Here is my Node js code -

const express = require ('express');
const app = express ();
const port = 8080;
const dbConfig = require ('./db.js');
const oracledb = require ('oracledb');


app.get ('/', (req, res) => {
  var jsonstring;
  oracledb.getConnection (dbConfig, function (err, connection) {
    if (err) throw err;
    oracledb.fetchAsString = [ oracledb.CLOB ];
    connection.execute (
      'select topic from table',
      {},
      function (err, result) {
        if (err) { console.error(err.message); return; }
      if (result.rows.length === 0)
        console.error("No results");

      else {
        var clob = result.rows[0][0];
        console.log(clob);

      }
        });
      }
    );
  });

app.listen (port, () => console.log (`Example app listening on port ${port}!`));

And Here is my console output -

{"Data":[{"Country":"A","OrderNo":"1","Method":" indoor ","WorkOrderNo":"1","Zipcode":"3","OriginalTimeSlot":"2019-12-","CurrentTimeSlot":"2019-12-13","Status":"","WOCreationDate":"2019-11-09","ModifactionDate":"2019-12-11","Dispatch":{"Status":"00","DispatchUnit":[]},"Par":{"Parcel":[{"Active":true,"Weight":284.572,"Volume":0.68,"Trackingstatus":"","Number":"1"},{"Active":true,"Weight":5.396,"Volume":0.01,"Trackingstatus":"","Number":"16"},{"Active":true,"Weight":362.777,"Volume":0.831321,"Trackingstatus":"","Number":"162"}]},"TimeSlotId":"7a2bb6eb-b7a2-4033-8485-0df4015a3938"},{"Country":"B","OrderNo":"162570460","Method":"customer ","WorkOrderNo":"2","Zipcode":"2","OriginalTimeSlot":"2019-11-21","CurrentTimeSlot":"2019-11-21","Status":"90","WOCreationDate":"2019-11-17","ModifactionDate":"2019-12-11","Dispatch":{"Status":"20","DispatchUnit":[{"Unit":"017","Time":"2019-11-20"}]},"Par":{"Parcel":[{"Active":true,"Weight":12.271,"Volume":0.047,"Trackingstatus":"90","Number":"00935297810003971677"},{"Active":true,"Weight":14.668,"Volume":0.038042,"Trackingstatus":"90","Number":"00935297810003971684"}]},"TimeSlotId":"e3fe1936-64f9-42de-b2c4-0ddf00529720"}]}

Now I want to create 2 different JSON file from this output like below -

JSONFILE1:

{"Country":"A","OrderNo":"1","Method":" indoor ","WorkOrderNo":"1","Zipcode":"3","OriginalTimeSlot":"2019-12-","CurrentTimeSlot":"2019-12-13","Status":"","WOCreationDate":"2019-11-09","ModifactionDate":"2019-12-11","Dispatch":{"Status":"00","DispatchUnit":[]},"Par":{"Parcel":[{"Active":true,"Weight":284.572,"Volume":0.68,"Trackingstatus":"","Number":"1"},{"Active":true,"Weight":5.396,"Volume":0.01,"Trackingstatus":"","Number":"16"},{"Active":true,"Weight":362.777,"Volume":0.831321,"Trackingstatus":"","Number":"162"}]},"TimeSlotId":"7a2bb6eb-b7a2-4033-8485-0df4015a3938"}

JSONFILE2:

{"Country":"B","OrderNo":"162570460","Method":"customer ","WorkOrderNo":"2","Zipcode":"2","OriginalTimeSlot":"2019-11-21","CurrentTimeSlot":"2019-11-21","Status":"90","WOCreationDate":"2019-11-17","ModifactionDate":"2019-12-11","Dispatch":{"Status":"20","DispatchUnit":[{"Unit":"017","Time":"2019-11-20"}]},"Par":{"Parcel":[{"Active":true,"Weight":12.271,"Volume":0.047,"Trackingstatus":"90","Number":"00935297810003971677"},{"Active":true,"Weight":14.668,"Volume":0.038042,"Trackingstatus":"90","Number":"00935297810003971684"}]},"TimeSlotId":"e3fe1936-64f9-42de-b2c4-0ddf00529720"}

How can I divide like this?


Solution

  • An example of the code to deal with the data once you have fetched it is:

    const fs = require('fs');
    
    // This is the data you said you had fetched
    const clob = {"Data":[{"Country":"A","OrderNo":"1","Method":" indoor ","WorkOrderNo":"1","Zipcode":"3","OriginalTimeSlot":"2019-12-","CurrentTimeSlot":"2019-12-13","Status":"","WOCreationDate":"2019-11-09","ModifactionDate":"2019-12-11","Dispatch":{"Status":"00","DispatchUnit":[]},"Par":{"Parcel":[{"Active":true,"Weight":284.572,"Volume":0.68,"Trackingstatus":"","Number":"1"},{"Active":true,"Weight":5.396,"Volume":0.01,"Trackingstatus":"","Number":"16"},{"Active":true,"Weight":362.777,"Volume":0.831321,"Trackingstatus":"","Number":"162"}]},"TimeSlotId":"7a2bb6eb-b7a2-4033-8485-0df4015a3938"},{"Country":"B","OrderNo":"162570460","Method":"customer ","WorkOrderNo":"2","Zipcode":"2","OriginalTimeSlot":"2019-11-21","CurrentTimeSlot":"2019-11-21","Status":"90","WOCreationDate":"2019-11-17","ModifactionDate":"2019-12-11","Dispatch":{"Status":"20","DispatchUnit":[{"Unit":"017","Time":"2019-11-20"}]},"Par":{"Parcel":[{"Active":true,"Weight":12.271,"Volume":0.047,"Trackingstatus":"90","Number":"00935297810003971677"},{"Active":true,"Weight":14.668,"Volume":0.038042,"Trackingstatus":"90","Number":"00935297810003971684"}]},"TimeSlotId":"e3fe1936-64f9-42de-b2c4-0ddf00529720"}]};
    
    for (const v of clob.Data) {
      const filename = "myfile" + v.Country + '.txt';
      fs.writeFileSync(filename, JSON.stringify(v) + '\n');
    }
    

    This loops through the Data array in the LOB data, and constructs a filename based on the Country name in each array entry.