I am wondering why I am getting my JSON response with escaped double quotes. What is the best practice for sending JSON response back to client?
The code
var express = require('express');
var server = express();
var country = '';
var dataStr = '[{"country_code" : "USA", "country_name" : "United States","bac_limit" : 0.80}, { "country_code" : "CAN", "country_name" : "United States","bac_limit":0.80}]';
connectToMongoDb();
server.get('/', function(req, res){
country = req.query.country;
res.json(dataStr);
});
server.listen(8080);
The Output
"[{\"country_code\" : \"USA\", \"country_name\" : \"United States\",\"bac_limit\" : 0.80}, { \"country_code\" : \"CAN\", \"country_name\" : \"United States\",\"bac_limit\":0.80}]"
JSON.stringify('[{"country_code" : "USA"}]')- as long as you are trying to stringify a string the result is what you get. The best response it just stringified arraydataStrcovers