user1523236
user1523236

Reputation: 1463

Include backslash in my test JSON - JavaScript

I am testing an application where I wish to input some text into a textfield and see if the application accepts it or raises an error. I am using postman and reading from a json data file with my different entries.

I wish to include an entry like thisL

{
  "name": "namewith\test",
},

However using this I get an error:

Unexpected control character at 1:25

I have tried this:

{
  "name": "namewith\\test",
},

this:

{
  "name": "namewith\/test",
},

this

 {
    "name": "namewith'\'test",
 },

How can I include this backslash and not have it recognized as an escape character?

Upvotes: 0

Views: 3724

Answers (2)

toto
toto

Reputation: 1188

May this idea help.

var customerData = document.getElementById("inputText").val();
var json = {
   name: customerData
}
var jsonString = JSON.stringify(json); //you get the content parsed

Upvotes: -1

Kevin
Kevin

Reputation: 369

The correct way is \\

Your answer: { "name": "namewith\\test", },

should work. It works for me. Is there an object after this? That trailing comma could be a problem - unlike JS, JSON doesn't accept an extra comma at the end.

What error are you getting for this option.

Upvotes: 2

Related Questions