Christopher Martinez
Christopher Martinez

Reputation: 827

Javascript JSON.stringify change my string that contains backslash

I have this string variable

iteration  = "Toolset\\Iteration 1"

But when I add the string to a JSON.stringify it changes the value adding two more backslashes Toolset\\\\Iteration 1

var data = JSON.stringify([
    {
      "op": "add",
      "path": "/fields/System.IterationPath",
      "value": iteration
    }
]);
//output: Toolset\\\\Iteration 1

I need the output to be Toolset\\Iteration 1

What is the best way of adding that particular string with 2 backslashes to a JSON variable?

Upvotes: 1

Views: 483

Answers (1)

Stewart Smith
Stewart Smith

Reputation: 1416

JSON stringify encodes the object into a string. The actual value of the JSON is seen after you decode it from the string. The fact that the stringified version has escaped your slashes won't affect the actual value after the decode.

Upvotes: 2

Related Questions