6

I've big JSON something like this:

{
  "EmployeeMaster": {
    "ImageContent": null,
    "ImageName": null,
    "EMP_PhotoPath": "E:\BBM0000000001comparison.png"
  }
}

I'm trying to parse it, but its not working due to slash in EMP_PhotoPath.

How can resolve this error ?

9
  • You need to escape the slash with a slash: [...]th":"E:\\BBM00000[...] Commented Feb 16, 2017 at 18:53
  • 1
    When you say "parse," do you mean JSON.parse? Because that works fine Commented Feb 16, 2017 at 18:55
  • 1
    What are you using to generate the JSON? Any standard library that generates the JSON should automatically escape the slash. Are you generating it by hand? Commented Feb 16, 2017 at 18:56
  • 2
    he wants to keep the \ Commented Feb 16, 2017 at 18:56
  • 1
    @MoxShah - Newtonsoft.JSONConvert has a bug in it if it's not escaping the slash.I doubt that, so I think your actual problem lies elsewhere. Commented Feb 16, 2017 at 19:04

2 Answers 2

7

var jsonString = String.raw`{"EmployeeMaster":{"ImageContent":null,"ImageName":null,"EMP_PhotoPath":"E:\BBM0000000001comparison.png"}}`;
jsonString = jsonString.replace("\\","\\\\");
var jsonObj = JSON.parse(jsonString);
alert(jsonObj.EmployeeMaster.EMP_PhotoPath);

You can Achieve this by doing something like this:

var jsonString = String.raw`{"EmployeeMaster":{"ImageContent":null,"ImageName":null,"EMP_PhotoPath":"E:\BBM0000000001comparison.png"}}`;
jsonString = jsonString.replace("\\","\\\\");
var jsonObj = JSON.parse(jsonString);

String.raw is a method you can use to get the original string without interpretation,

It's used to get the raw string form of template strings (that is, the original, uninterpreted text).

So you can replace the backslash with double backslashes, then you can parse it to keep the original backslash.

Sign up to request clarification or add additional context in comments.

4 Comments

Manually manipulating a JSON string generated by a standard library is a bad idea.
Agreed with Adam, its not a good option to play with, but as of now its a good work around for me. @Adam Do you see any major risk in above solution ?
@Ryad I'm getting an error while using this String.raw method Cannot convert undefined or null to object Function.raw (native) this is what I got when I actually put big JSON into action.
@MoxShah - pour over the docs of Newtonsoft.JSONConvert (and google) there's a way to escape your backslash using the library, I'm just not familiar with it. There's no way that everybody who uses this library manually manipulates their strings afterwards to handle the same situation you have encountered.
1

You have to escape the slash with a second slash. Your valid json would look like that:

{
    "EmployeeMaster": {
        "ImageContent": null,
        "ImageName": null,
        "EMP_PhotoPath": "E:\\BBM0000000001comparison.png"
    }
}

ps: Paste it into JSONLint.com to verifiy.

1 Comment

I know, double slash will do the trick, but how can add double slash ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.