如何重新格式化oracledb json输出?

时间:2017-11-01 12:09:08

标签: node.js oracledb

使用带有outFormat的oracledb node.js驱动程序:oracledb.OBJECT选项返回json,但是coloumn名称形成大写(属性名称遵循Oracle的标准名称 - 大小写规则),如下所示: { “ID” 为 “1”} 是否可以将它们设为小写,如下所示: { “ID” 为 “1”}?

JSON_OBJECT介绍在Oracle Database 12.2中我无法使用。

1 个答案:

答案 0 :(得分:2)

只需使用列别名:

const oracledb = require('oracledb');
const config = require('./dbConfig.js');

(async function() {
  let conn;
  let result;

  try {
    conn = await oracledb.getConnection(config);

    result = await conn.execute(
     `select first_name || ' ' || last_name name,
        email
      from employees
      where rownum = 1`,
      [], // no binds
      {
        outFormat: oracledb.OBJECT
      }
    );

    // This is the problem, uppercase column names, no?
    console.log(result.rows); // [ { NAME: 'Steven King', EMAIL: 'SKING' } ]

    result = await conn.execute(
     `select first_name || ' ' || last_name "name",
        email "email"
      from employees
      where rownum = 1`,
      [], // no binds
      {
        outFormat: oracledb.OBJECT
      }
    );

    // Here's the result with case sensitve aliases
    console.log(result.rows); // [ { name: 'Steven King', email: 'SKING' } ]
  } catch (err) {
    // Will throw, but only after finally runs
    throw err; 
  } finally {
    if (conn) {
      try {
        await conn.close();
      } catch (err) {
        console.log('error closing conn', err);
      }
    }
  }
}());

或"流程"之后的结果。您可能会发现这相关: https://jsao.io/2015/07/relational-to-json-with-node-js/