Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issue sending over JSON as string from POSTMAN to node server

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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"
   }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading