I cannot find any documentation on express.json()
and express.urlencoded()
. What do each of them do exactly?

- 14,010
- 29
- 101
- 161

- 2,281
- 4
- 16
- 16
-
Express v4 doc links... [express.json()](https://expressjs.com/en/4x/api.html#express.json) and [express.urlencoded()](https://expressjs.com/en/4x/api.html#express.urlencoded) – Phil May 25 '23 at 00:57
7 Answers
Here is the explanation that should clear doubts on express.json()
and express.urlencoded()
and the use of body-parser. It took me some time to figure this out.
What is Middleware? It is those methods/functions/operations that are called BETWEEN processing the Request and sending the Response in your application method.
When talking about
express.json()
andexpress.urlencoded()
think specifically about POST requests (i.e. the .post request object) and PUT Requests (i.e. the .put request object)You DO NOT NEED
express.json()
andexpress.urlencoded()
for GET Requests or DELETE Requests.You NEED
express.json()
andexpress.urlencoded()
for POST and PUT requests, because in both these requests you are sending data (in the form of some data object) to the server and you are asking the server to accept or store that data (object), which is enclosed in the body (i.e.req.body
) of that (POST or PUT) RequestExpress provides you with middleware to deal with the (incoming) data (object) in the body of the request.
a.
express.json()
is a method inbuilt in express to recognize the incoming Request Object as a JSON Object. This method is called as a middleware in your application using the code:app.use(express.json());
b.
express.urlencoded()
is a method inbuilt in express to recognize the incoming Request Object as strings or arrays. This method is called as a middleware in your application using the code:app.use(express.urlencoded());
ALTERNATIVELY, I recommend using body-parser (it is an NPM package) to do the same thing. It is developed by the same peeps who built express and is designed to work with express. body-parser used to be part of express. Think of body-parser specifically for POST Requests (i.e. the .post request object) and/or PUT Requests (i.e. the .put request object).
In body-parser you can do
// calling body-parser to handle the Request Object from POST requests var bodyParser = require('body-parser'); // parse application/json, basically parse incoming Request Object as a JSON Object app.use(bodyParser.json()); // parse application/x-www-form-urlencoded, basically can only parse incoming Request Object if strings or arrays app.use(bodyParser.urlencoded({ extended: false })); // combines the 2 above, then you can parse incoming Request Object if object, with nested objects, or generally any type. app.use(bodyParser.urlencoded({ extended: true }));

- 5,503
- 2
- 16
- 9
-
94If you are using Express >= 4.16.0, body parser has been re-added under the methods `express.json()` and `express.urlencoded()`. – David Ferreira Mar 16 '19 at 17:58
-
21`app.use(bodyParser.urlencoded({ extended: true }));` does not combine `app.use(bodyParser.json());` and `app.use(bodyParser.urlencoded({ extended: false }));`: it does not handle application/json but only application/x-www-form-urlencoded. The `extended` options is used to specify [whether bodyParser should parse URL-encoded data with `qs` (`extended: true`) or `querystring` (`extended: false`)](https://github.com/expressjs/body-parser#extended). – kimamula Jun 09 '19 at 04:14
-
6body-parser is deprecated. We should be using the inbuilt express methods. – Rap Mar 17 '21 at 15:04
-
-
Leave 6 and 7 out for [zero warnings](https://stackoverflow.com/a/24344756/1705829) – Timo Mar 25 '21 at 20:56
-
1@VirajSingh it means that the bodyParser will use the lib [qs](https://www.npmjs.com/package/qs#readme) if set as false it will use the [querystring](https://nodejs.org/api/querystring.html) – Kleber Germano Apr 15 '21 at 20:36
-
1@Kleber Germano In which scenario {extend: true} option or using 'qs' over 'querystring' will be preferable ? – Viraj Singh Apr 16 '21 at 14:14
-
It's not clear to me from this answer why you would use bodyparser over the built in middlewares – Tom Sep 30 '21 at 10:17
-
Wait, so `express.urlencoded({ extended: true })` includes `express.json()` in itself? – Newbie... Aug 06 '22 at 18:19
-
@Tom Back I think back when this question was asked they weren't built-in. – Newbie... Aug 06 '22 at 18:21
-
Why does express allow defining functions with a called function? Normally if you are defining a function or passing as an argument you pass the reference and not the call. I expected it to be, `app.use(express.json);` but instead I see things like, `app.use(express.json());` – 1.21 gigawatts May 18 '23 at 16:43
If you ask me "what is the difference between express.urlencoded({extended: false})
and express.json()
", well, the difference is:
express.json()
If you use express.json()
it will parse the body from post/fetch request except from html post form. It wont parse information from the html post form :
<form action="/" method="POST">
<input type="text" name="username">
<button>Submit</button>
</form>
For instance, if you fill the form with "dean" then submit it, Express wont have an idea what inside the body with this express code:
const express = require('express')
const app = express()
app.use(express.json())
// app.use(express.urlencoded({ extended: false }))
app.use(express.static("public"))
app.get("/", (req, res) => {
res.sendFile("index.html")
})
app.post("/", (req, res) => {
res.send(req.body)
})
const port = process.env.PORT || 3001
app.listen(port, () => {
console.log(`Server Up in Port ${port}`);
})
It will send {}
after you click submit. But if you uncommented app.use(express.urlencoded({extended: false}))
,
then you will get {"username": "dean"}
.
So the difference is express.json()
is a body parser for post request except html post form and express.urlencoded({extended: false})
is a body parser for html post form.

- 559
- 4
- 7
-
2yes but fetch does not only send data in json format; it may also sent urlencoded data – Minsky Dec 24 '21 at 10:06
The json
and urlencoded
middleware are both part of bodyParser. This is what the README says:
bodyParser([options])
Returns middleware that parses both
json
andurlencoded
. Theoptions
are passed to both middleware.bodyParser.json([options])
Returns middleware that only parses
json
. The options are:
strict
- only parse objects and arrayslimit
<1mb> - maximum request body sizereviver
- passed toJSON.parse()
bodyParser.urlencoded([options])
Returns middleware that only parses
urlencoded
with the qs module. The options are:
limit
<1mb> - maximum request body size

- 139,544
- 27
- 275
- 264
-
3Just remembering that bodyParser.urlencoded with the option `extended = false` will use the library [querystring](https://nodejs.org/api/querystring.html) e.g:`bodypParser.urlencoded({ extended: false }) //querystring` and the the library [qs](https://www.npmjs.com/package/qs#readme) if the extended option was set true e.g:`bodypParser.urlencoded({ extended: true }) //qs` source [here](https://github.com/expressjs/body-parser#bodyparserurlencodedoptions) – Kleber Germano Apr 15 '21 at 19:59
What is Middleware
To understand what express.json and express.urlencoded do, you have to understand what middlewares are.
Middlewares are functions or methods in expressJS for carrying out various operations on requests made to the server.
By now you should know how to get a request to a route using express.
app.get("/api/houses", (req, res) => {
console.log("Received request");
res.send("houses")
})
Importance of Middleware
The above code is a typical example of how express handles a get request. But in a situation whereby you want an operation to be carried on every request made to the server. You would not like to repeat codes in every route.
A middleware comes to the rescue at this stage. The middleware acts like a general reciever for every request.
app.use((req, res, next) => {
console.log("Verifing request");
next();
})
The above is a custom middleware that verifies every request made to my server and sends ad sends the request too the next appropriate routeing middleware depending on the type of request it. (GET, POST, PUT etc.)
Builtin Middleware
Now expressJS has some already made middlewares which help developer in carrying out some tedious task. like converting request body to JSON and so many others.
Examples of these builtin ExpressJS middlewares are
- express.json()
- express.urlencoded()
express.json() is a built express middleware that convert request body to JSON.
express.urlencoded() just like express.json() converts request body to JSON, it also carries out some other functionalities like: converting form-data to JSON etc.

- 487
- 6
- 8
-
so if i use express.urlencoded() , do i need to again add express.json() ? – anandhu Jul 28 '21 at 11:50
-
1
This middleware is available in Express v4.16.0 onwards.
app.use(express.urlencoded({ extended: true}))
extended[boolean]: This option allows to choose between parsing the URL-encoded data with the querystring library (when false) or the qs library (when true). The “extended” syntax allows for rich objects and arrays to be encoded into the URL-encoded format, allowing for a JSON-like experience with URL-encoded
express docs :- https://expressjs.com/en/api.html#express.urlencoded

- 281
- 2
- 8
express.urlencoded() is required when you are submitting a form with post method ( default content-type = application/ x-www-form-urlencoded ) and you need to acess that form data using req.body , if you don't use it req.body would be undefined.

- 445
- 6
- 9
In simple terms, the main difference between express.json()
and app.use(express.urlencoded({ extended: true }))
in the Express.js framework is how they handle incoming data in the request body.
if your client sends data to the server as JSON, you should use express.json()
to parse and access that data.
If your client sends data using HTML forms or URL-encoded format, you should use express.urlencoded({ extended: true })
to parse and access that data. Choose the appropriate middleware based on the data format you expect to receive from the client.

- 706
- 4
- 15