An-droid An-droid - 1 year ago 186
JSON Question

Getting JSON formatted response with oracledb npm and nodejs

I'm requesting data using oracledb npm trying to get a JSON formatted response

here is an example of select block I'm using :

const block =
'BEGIN ' +
':response := PK.getData(:param);' +
'END;';


Called using :

const result = await connection.execute(block, bindVars);


Then later using express :

res.status(200).send(result);


In this configuration I get a response with the good data BUT postman tells me that it is a string when it should see it as JSON

{ "metadata":[{"client":"name"...


If I use .json() :

res.status(200).json(result);


I get this kind of response with double quotes and escaped chars :

"{ \"metadata\":[{\"client\":\"name\"...


Any idea how to solve this ?

Answer Source

You need to do JSON.parse on your response, because you can send only string as response from server

Edit:

Add Content-Type: application/json header so it will be auto parsed in Postman

res.header('Content-Type' , 'application/json');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download