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

Mit*_*ich 3 oracle node.js node-oracledb

使用带有 outFormat:oracledb.OBJECT 选项的 oracledb node.js 驱动程序会返回 json,但列名格式为大写(属性名遵循 Oracle 的标准命名规则),例如:{"ID":"1"} 是否可以把它们变成小写,像这样:{"Id":"1"}?

在 Oracle Database 12.2 中引入的 JSON_OBJECT 对我不可用。

Dan*_*han 5

只需使用列别名:

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);
      }
    }
  }
}());
Run Code Online (Sandbox Code Playgroud)

或者“处理”之后的结果。您可能会发现这相关:https : //jsao.io/2015/07/relational-to-json-with-node-js/