Express res.json() Function
Last Updated :
07 Jan, 2025
The res.json() function sends a JSON response. This method sends a response (with the correct content-type) that is the parameter converted to a JSON string using the JSON.stringify() method.
Syntax:
res.json( [body] )
Parameters: The body parameter is the body that is to be sent in the response.
Return Value: It returns an Object.
Steps to Install the Express Module:
Step 1: You can install this package by using this command.
npm install express
Step 2: After installing the express module, you can check your express version in the command prompt using the command.
npm version express
Project Structure:
Project StructureThe updated dependencies in package.json file will look like:
"dependencies": {
"express": "^4.18.2",
}
Example 1: Below is the code of res.json() Function implementation.
javascript
const express = require('express');
const app = express();
const PORT = 3000;
// Without middleware
app.get('/', function (req, res) {
res.json({ user: 'geek' });
});
app.listen(PORT, function (err) {
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Steps to run the program:
Run the index.js file using the below command:
node index.js
Console Output:
Server listening on PORT 3000
Browser Output: Now open the browser and go to http://localhost:3000/, now on your screen you will see the following output:
{"user":"geek"}
Example 2: Below is the code of res.json() Function implementation.
javascript
const express = require('express');
const app = express();
const PORT = 3000;
// With middleware
app.use('/', function (req, res, next) {
res.json({ title: "GeeksforGeeks" })
next();
})
app.get('/', function (req, res) {
console.log("User Page")
res.end();
});
app.listen(PORT, function (err) {
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Steps to run the program:
Run the index.js file using the below command:
node index.js
Browser Output: Now open a browser and go to http://localhost:3000/, now on your screen you will see the following output:
{"title":"GeeksforGeeks"}
Console Output: And you will see the following output on your console:
User Page
Similar Reads
ExpressJS express.json() Function The express.json() function is a built-in middleware in Express that is used for parsing incoming requests with JSON payload. The express.json middleware is important for parsing incoming JSON payloads and making that data available in the req.body or further processing within the routes. Without us
4 min read
Difference between res.send() and res.json() in Express.js? In this article, we will learn about res.send() & res.json(), along with discussing the significant distinction that differentiates between res.send() & res.json(). Let us first understand what is res.send() and res.json() in Express.js? res.send() - The res.send() function is used for sendi
4 min read
Express.js res.jsonp() Function The res.jsonp() function is used to send a JSON response with JSONP support and this function is similar to the res.json() function except that it opts-in to the support of JSONP callback. Syntax:Â res.jsonp( [body] ) Parameter: The body parameter describes the body type which can be sent in respons
2 min read
Express res.send() Function The res.send function is used to send a response in form of various types of data, such as strings, objects, arrays, or buffers, and automatically sets the appropriate Content-Type header based on the data type.res.send in ExpressThe res.send() function sends the HTTP response. The body parameter ca
3 min read
How to Generate or Send JSON Data at the Server Side using Node.js ? In modern web development, JSON (JavaScript Object Notation) is the most commonly used format for data exchange between a server and a client. Node.js, with its powerful runtime and extensive ecosystem, provides robust tools for generating and sending JSON data on the server side. This guide will wa
3 min read
Built-in Middleware Functions in Express.js Express.js is a Node.js framework used to develop the backend of web applications. While you can create custom middleware in Express.js using JavaScript, Express.js also provides several built-in middleware functions for use in Express applications. Middleware are functions that are executed when ca
4 min read