1

I have the string with backslashes:

const str = 'E:\\music\\post'; // It represents "E:\music\post" text in HTML

JSON.stringify(str) returns "the same" string '"E:\\music\\post"'. Why not '"E:\\\\music\\\\post"'?


JSON.parse('"E:\\music\\post"') will throws the error: Uncaught SyntaxError: Unexpected token m in JSON at position 4.

JSON.parse('"E:\\\\music\\\\post"') works OK.

But another strange thing is that JSON.parse(JSON.stringify("E:\\music\\post")) works well. Why?


That is the better workaround for it? I want to save the string to json file, and later to create an object from the json file.

KeyKi
  • 2,693
  • 3
  • 12
  • 24
  • The backslash is an escape character in both JS string literals and in JSON. There is only one literal backslash in your `str`. When stringified, the backslash is doubled (from 1 to 2) to indicate a literal backslash in the string, rather than an escape character. – CertainPerformance Oct 14 '19 at 23:09
  • `JSON.parse('"E:\\music\\post"')` is ``JSON.parse(String.raw`"E:\music\post"`)``. Invalid escape sequences (like `\m`) are syntax errors when parsing JSON. – CertainPerformance Oct 14 '19 at 23:10
  • Well, I just will remember that for getting a valid JSON string (for copypasting and saving the string to a JSON file) I need always to perform an addition action: `JSON.stringify(str).replace(/\\/g, "\\\\")`. There are no others caveats like this one? – KeyKi Oct 14 '19 at 23:43
  • That doesn't make sense to me - by manually doubling up on every backslash, you're removing every escape sequence. Eg, if the raw stringified string is `"foo\nbar"` (which, when parsed, is a string containing a newline) you're turning it into (raw) `"foo\\nbar"` (which, when parsed, is a string containing a literal backslash followed by the character `n`). – CertainPerformance Oct 14 '19 at 23:47
  • Check this code: https://pastebin.com/VgjWR9Tu Without manually doubling backslashes it makes a non-valid JSON string. `JSON.parse(contentOfOneOfJSONFiles)` will throw the exception. – KeyKi Oct 15 '19 at 00:10
  • It looks valid to me. The downloaded JSON is (raw) `"E:\\music\\post"`, which, when parsed, is exactly the same as your original string: ``JSON.parse(String.raw`"E:\\music\\post"`) === 'E:\\music\\post'`` – CertainPerformance Oct 15 '19 at 00:14
  • Here is it. https://jsbin.com/qopebu/edit?html,console,output Yes, backslash doubling is not needed for working with files too. It helps only if you manually copypasting the text to the browser's console. (What I did before) – KeyKi Oct 15 '19 at 01:02
  • 1
    Using the browser console can be difficult when you want to input raw data because everything gets interpreted in terms of Javascript and its escape characters. Either input the raw data some other way (like with a file, or with a network request, or with local storage), or use `String.raw` so that the string gets interpreted completely literally (for the most part) – CertainPerformance Oct 15 '19 at 01:09

0 Answers0