Home > Software design >  Issue sending over JSON as string from POSTMAN to node server
Issue sending over JSON as string from POSTMAN to node server

Time:06-16

I have some json that looks like this:

const json = {
   "name": "Peter",
   "age": 21
}

Then I call JSON.stringify(json) which gives me the following:

'{"name":"Peter","age":21}'

I want to send data from the UI to my server. Currently I am using POSTMAN for this.

POST
http:localhost:8888/user
Body -> JSON

{
  "user": { 
     "details": '{"name":"Peter","age":21}',
     "foo": "bar"
   }
}

details here needs to be a string but when I send over the data I get the following error on the server

SyntaxError: Unexpected token } in JSON at position 114

Does anyone have any ideas here?

CodePudding user response:

JSON does not allow for single quotes, what you are trying to send is invalid. What you need to do is use double quotes and escape the internal quotes for the JSON key/values:

{
  "user": { 
     "details": "{\"name\":\"Peter\",\"age\":21}",
     "foo": "bar"
   }
}
  • Related