Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to return JSON using node or Express

So, one can attempt to fetch the following JSON object:

$ curl -i -X GET http://echo.jsontest.com/key/value/anotherKey/anotherValue HTTP/1.1 200 OK Access-Control-Allow-Origin: * Content-Type: application/json; charset=ISO-8859-1 Date: Wed, 30 Oct 2013 22:19:10 GMT Server: Google Frontend Cache-Control: private Alternate-Protocol: 80:quic,80:quic Transfer-Encoding: chunked  {    "anotherKey": "anotherValue",    "key": "value" } $ 

Is there a way to produce exactly the same body in a response from a server using node or express? Clearly, one can set the headers and indicate that the content-type of the response is going to be "application/json", but then there are different ways to write/send the object. The one that I have seen commonly being used is by using a command of the form:

response.write(JSON.stringify(anObject)); 

However, this has two points where one could argue as if they were "problems":

  • We are sending a string.
  • Moreover, there is no new line character in the end.

Another idea is to use the command:

response.send(anObject); 

This appears to be sending a JSON object based on the output of curl similar to the first example above. However, there is no new line character in the end of the body when curl is again being used on a terminal. So, how can one actually write down something like this with a new line character appended in the end using node or node/express?

like image 360
MightyMouse Avatar asked Oct 31 '13 00:10

MightyMouse


People also ask

How do I return JSON data in node JS?

stringify() to return JSON data): We will now use http. createServer() and JSON. stringify() to return JSON data from our server.

Is Express better than node?

Why do developers prefer NodeJS over Express? NodeJS is an event-driven, non-blocking I/O model using JavaScript as its main language. It helps to build scalable network applications. Express is a minimal and flexible Node.

What is the correct way to write a JSON data?

JSON - SyntaxData is represented in name/value pairs. Curly braces hold objects and each name is followed by ':'(colon), the name/value pairs are separated by , (comma). Square brackets hold arrays and values are separated by ,(comma).

Why we use express JSON in node JS?

json() is a built-in middleware function in Express. This method is used to parse the incoming requests with JSON payloads and is based upon the bodyparser. This method returns the middleware that only parses JSON and only looks at the requests where the content-type header matches the type option.


2 Answers

That response is a string too, if you want to send the response prettified, for some awkward reason, you could use something like JSON.stringify(anObject, null, 3)

It's important that you set the Content-Type header to application/json, too.

var http = require('http');  var app = http.createServer(function(req,res){     res.setHeader('Content-Type', 'application/json');     res.end(JSON.stringify({ a: 1 })); }); app.listen(3000);  // > {"a":1} 

Prettified:

var http = require('http');  var app = http.createServer(function(req,res){     res.setHeader('Content-Type', 'application/json');     res.end(JSON.stringify({ a: 1 }, null, 3)); }); app.listen(3000);  // >  { // >     "a": 1 // >  } 

I'm not exactly sure why you want to terminate it with a newline, but you could just do JSON.stringify(...) + '\n' to achieve that.

Express

In express you can do this by changing the options instead.

'json replacer' JSON replacer callback, null by default

'json spaces' JSON response spaces for formatting, defaults to 2 in development, 0 in production

Not actually recommended to set to 40

app.set('json spaces', 40); 

Then you could just respond with some json.

res.json({ a: 1 }); 

It'll use the 'json spaces' configuration to prettify it.

like image 129
bevacqua Avatar answered Sep 20 '22 04:09

bevacqua


Since Express.js 3x the response object has a json() method which sets all the headers correctly for you and returns the response in JSON format.

Example:

res.json({"foo": "bar"}); 
like image 33
JamieL Avatar answered Sep 20 '22 04:09

JamieL