Deepak Das
Deepak Das

Reputation: 183

why body-parser.json() is not working ? showing SyntaxError: Unexpected token } in JSON at position 100 at JSON.parse (<anonymous>)

Please explain to me where I m doing mistake. why my body is not parsing in JSON format

I m using postman for my post request...(Header type is mention below)

[{"key":"Content-Type","name":"Content-Type","value":"application/json","description":"","type":"text"}]

Thank you for looking to my post I will appreciate your contribution

const express=require('express');
const bodyParser=require('body-parser');

const app=express();

const port= process.env.PORT || 3002;

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));


app.post('/send',(req,res)=>{
    res.json(req.body);

})

app.listen(port,()=>{
    console.log(`listing to port ${port}`)
});

SyntaxError: Unexpected token } in JSON at position 100
    at JSON.parse (<anonymous>)
    at parse (C:\Users\Deepak Das\Desktop\node test\test\node_modules\body-parser\lib\types\json.js:89:19)
    at C:\Users\Deepak Das\Desktop\node test\test\node_modules\body-parser\lib\read.js:121:18
    at invokeCallback (C:\Users\Deepak Das\Desktop\node test\test\node_modules\raw-body\index.js:224:16)
    at done (C:\Users\Deepak Das\Desktop\node test\test\node_modules\raw-body\index.js:213:7)
    at IncomingMessage.onEnd (C:\Users\Deepak Das\Desktop\node test\test\node_modules\raw-body\index.js:273:7)
    at IncomingMessage.emit (events.js:194:15)
    at endReadableNT (_stream_readable.js:1125:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)

Upvotes: 1

Views: 2863

Answers (2)

Fabio Valencio
Fabio Valencio

Reputation: 21

This message: Unexpected token ** } ** in JSON at position 100 means that the } character is in the wrong position

Upvotes: 0

Luck1609
Luck1609

Reputation: 344

Just in case someone comes across this later. I figured I had made an error with the request in the body of postman like so, { "comment": "Some random comment", "author": "Lucky", }. The error was as a result of the last comma (,) at the end of the author. So all I had to do was make that correction and everything just worked.

Upvotes: 1

Related Questions