Send a JSON response using Express Framework

Generally, we use res.send() method to send a response to the API call. Here is the sample code.

const express = require('express');
const router = express.Router();
const app = express();

router.get('/api', (req, res) => {
  res.send('Hello World');
});

app.use('/', router);
app.listen(3000);

Express res.json() method for sending JSON response

You can send the JSON response by using res.json() method. It accepts an object and converts it into JSON before sending it as a response.

Syntax:

res.json(data);

data can be a JavaScript object or a value you want to convert into JSON and send as a response to the clients.

Return:

The res.json() method returns a  promise to get the resolved JSON data.

Example:

const express = require('express');
const router = express.Router();
const app = express();

router.get('/api', (req, res) => {
  res.json({ message: 'Hello World' });
});

app.use('/', router);
app.listen(3000);

Looking for more Express framework tutorials? Learn from our DIY Express tutorials by building awesome apps.

Frequently Asked Questions (FAQs)

How to send JSON data using Express?

You can use the Express res.json() method to send JSON data in Node.js. You just have to call the res.json() method and pass a JavaScript object or a value, this method will automatically convert it into JSON, set the Content-Type header and send it as a response to the respective request.

What is the difference between send and JSON in Express?

The main difference between these two is that the send() method sets the Content-Type as “text/html” whereas the json() method sets it as “application/json” and can convert the data into a JSON string before sending it as a response object.

How to send a variable in JSON?

You can send a variable by wrapping it in a JS object as key-value pair and passing it as an argument to the Express.js res.json() method.

How to send array data in JSON?

For sending array data using the res.json() method, you have to create a JavaScript object with a key having the array you want to send.

What is JSON full form? 

JSON stands for JavaScript Object Notation, used to structure data based on JavaScript object syntax.

How to write a JSON string?

You can create a JavaScript object and pass it as an argument to JSON.stringify() function to convert it into JSON string.

How to put data in JSON format?

To put data in JSON format, you have to first wrap it into an object, then convert it into JSON string using JSON.stringify() function.

Conclusion

To send data as a response to a client we typically use res.send() method, and to send static files we use the res.sendFile() method. In the same way, to send JSON data, the Express module has a method res.json() that takes an object or a value, converts it into JSON and sends it as a response. You don’t even have to set the Content-Type header, it will automatically set it to “application/json”.  This method is super useful and easy, you should definitely try it next time when you are building an application where it is required to send JSON data.

Reference

https://stackoverflow.com/questions/19696240/proper-way-to-return-json-using-node-or-express

Pankaj Kumar
Pankaj Kumar
Articles: 208