简体   繁体   中英

Oracle DB select query returning the data in form of array instead of json array using nodejs

My DB select result

{
        "metaData": [
            {
                "name": "CUSTOMERID"
            },
            {
                "name": "NAME"
            },
            {
                "name": "EMAILID"
            },
            {
                "name": "PHONE_NUMBER"
            },
            {
                "name": "CREATED_AT"
            },
            {
                "name": "ACC_STATUS"
            }
        ],
        "rows": [
            [
                "62c697be-b0b8-4f90-a014-149c1c175303",
                "ratan uday kumar",
                "uday@evontex.com",
                "+91781891",
                "2018-06-04T10:20:55.505Z",
                0
            ]
        ]
    }

Expected Data

[
    {
        "CUSTOMERID": "62c697be-b0b8-4f90-a014-149c1c175303",
        "NAME": "ratan uday kumar",
        "EMAILID": "uday@evontex.com",
        "PHONE_NUMBER": "+91781891",
        "CREATED_AT": "2018-06-04T10:20:55.505Z",
        "ACC_STATUS": 0
    }
]

I am using nodejs oracledb package

Is there any method to get result in json array or do manually i have to write json array function???

The Answer is by setting the Output format of response to object provided by @ torsten link

var oracledb = require('oracledb');
oracledb.outFormat = oracledb.OBJECT;

The above answer have worked for me

You most probably look for "oracledb.outFormat". Check the documentation of this feature.

3.2.14 oracledb.outFormat

The format of query rows fetched when using connection.execute() or connection.queryStream().
It affects both ResultSet and non-ResultSet queries.
It can be used for top level queries and REF CURSOR output.

This can be either of the Oracledb constants oracledb.OUT_FORMAT_ARRAY or oracledb.OUT_FORMAT_OBJECT. The default value is oracledb.OUT_FORMAT_ARRAY which is more efficient. The older, equivalent constants oracledb.ARRAY and oracledb.OBJECT are deprecated.

Just add one line should already help:

var oracledb = require('oracledb');
oracledb.outFormat = oracledb.OUT_FORMAT_OBJECT;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM