Slinky Slinky - 1 year ago 106
JSON Question

Node Express & JSON Response Format

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}]';

server.get('/', function(req, res){
country =;

The Output

"[{\"country_code\" : \"USA\", \"country_name\" : \"United States\",\"bac_limit\" : 0.80}, { \"country_code\" : \"CAN\", \"country_name\" : \"United States\",\"bac_limit\":0.80}]"

Answer Source

Your dataStr is actually a string, and res.json call is sending the string as such. If you wish to send the data as JSON, don't put it as a string, but a JS object/array (or use JSON.parse):

var dataStr = [{"country_code" : "USA", "country_name" : "United States","bac_limit" : 0.80}, { "country_code" : "CAN", "country_name" : "United States","bac_limit":0.80}]
// alternatively, JSON.parse(dataStr)

server.get('/', function(req, res){
 country =
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download