我正在尝试使用Node js将单个Oracle CLOB数据转换为多个JSON文件。我已经使用oracledb.fetchAsString = [ oracledb.CLOB ];
来获取Oracle CLOB数据。这是我的Node js代码-
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}!`));
这是我的控制台输出-
{"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"}]}
现在,我想从此输出中创建2个不同的JSON文件,如下所示-
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"}
我该如何分割?
const fs = require('fs');
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');
}