使用 Node JS 将 Oracle CLOB 划分为多个 JSON
Divide Oracle CLOB to Multiple JSON with Node JS
我正在尝试使用 Node js 将单个 Oracle CLOB 数据转换为多个 JSON 文件。我使用 oracledb.fetchAsString = [ oracledb.CLOB ];
来获取 Oracle CLOB 数据。这是我的节点 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 文件,如下所示 -
JSON文件 1:
{"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"}
JSON文件 2:
{"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');
// 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');
}
这会遍历 LOB 数据中的 Data
数组,并根据每个数组条目中的国家/地区名称构造一个文件名。
您得到的结果是一个很大的 JSON 字符串。首先,您需要将它解析为一个 JavaScript 对象,从那里,您可以轻松地使用该对象来获得您需要的东西。
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);
const obj = JSON.parse('clob', clob);
const file1 = obj.data[0];
console.log('file1', file1);
const file2 = obj.data[1];
console.log('file2', file2);
}
});
}
);
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
我刚刚向您展示了如何获取您想要的文件内容。如您所见,数据 属性 (一旦 JSON 内容被解析)是一个数组。您可以像我一样使用硬编码索引访问元素,也可以像 Chris 那样使用循环。
如果您需要将内容写入 Node.js 服务器上的实际文件,那么 Chris 的回答应该有所帮助。
我正在尝试使用 Node js 将单个 Oracle CLOB 数据转换为多个 JSON 文件。我使用 oracledb.fetchAsString = [ oracledb.CLOB ];
来获取 Oracle CLOB 数据。这是我的节点 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 文件,如下所示 -
JSON文件 1:
{"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"}
JSON文件 2:
{"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');
// 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');
}
这会遍历 LOB 数据中的 Data
数组,并根据每个数组条目中的国家/地区名称构造一个文件名。
您得到的结果是一个很大的 JSON 字符串。首先,您需要将它解析为一个 JavaScript 对象,从那里,您可以轻松地使用该对象来获得您需要的东西。
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);
const obj = JSON.parse('clob', clob);
const file1 = obj.data[0];
console.log('file1', file1);
const file2 = obj.data[1];
console.log('file2', file2);
}
});
}
);
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
我刚刚向您展示了如何获取您想要的文件内容。如您所见,数据 属性 (一旦 JSON 内容被解析)是一个数组。您可以像我一样使用硬编码索引访问元素,也可以像 Chris 那样使用循环。
如果您需要将内容写入 Node.js 服务器上的实际文件,那么 Chris 的回答应该有所帮助。