ExpressJS express.json() Function (original) (raw)

Last Updated : 20 Sep, 2025

The express.json() is a built-in middleware in Express. It helps your app read JSON data sent from the client (like in POST or PUT requests) and makes it available in req.body. Without it, Express cannot understand JSON data in requests.

**Use Cases:

**Syntax:

ExpressJSon( [options] )

**In the above syntax:

How express.json() Works?

**Steps To Use ExpressJSon() Function

**Step 1: Create a Node.js application using the following command.

mkdir nodejs cd nodejs npm init -y

**Step 2: Install the required dependencies.

npm install express

**Step 3: Create the required files and start the server.

node index.js

**Handling JSON Data in ExpressJS

JavaScript `

// Filename - index.js

const express = require('express'); const app = express(); const PORT = 3000;

app.use(express.json());

app.post('/', function (req, res) { console.log(req.body.name); res.end(); });

app.listen(PORT, function (err) { if (err) console.log(err); console.log("Server listening on PORT", PORT); });

`

**Output

When a POST request is made to http://localhost:3000/ with the header Content-Type: application/json and the body {"name":"GeeksforGeeks"}, the following output is displayed on the console:

output

**In this example

**Without Using ExpressJSON()

JavaScript `

// Filename - index.js const express = require('express'); const app = express(); const PORT = 3000; // Without this middleware // app.use(express.json()); app.post('/', function (req, res) { console.log(req.body.name); res.end(); }); app.listen(PORT, function (err) { if (err) console.log(err); console.log("Server listening on PORT", PORT); });

`

**Output:

When a POST request is made to http://localhost:3000/ with the header Content-Type: application/json and the body {"name":"GeeksforGeeks"}, the following output is displayed on the console:

output

**In this example