-2

the value includes back slash and slash, I'd like to keep the format

NOTES: I don't want to change the original json file as some answers to ask to escape the slash first. You never know which value should be escape, which should not. The sample is simple, but the real json file is bigger. I just need same data to be read and output as exact same.

$ cat a.json
{
  "name": "url",
  "value": "https:\/\/www.example.com"
}

but when export its value, the back slash is gone.

$ cat a.js
var fs = require("fs");
var content = fs.readFileSync("a.json");
var json = JSON.parse(content);
console.log(json)

fs.writeFile("output", json.value, function(err) {
    if(err) {
        return console.log(err);
    }
    console.log("The file was saved!");
});

Here is the test result

$ node a.js
{ name: 'url', value: 'https://www.example.com' }
The file was saved!

$ cat output
https://www.example.com

Which I still want to save the original value to the file

https:\/\/www.example.com

How to do that?

BMW
  • 42,880
  • 12
  • 99
  • 116

2 Answers2

0

The backslash is used as an escape character. For example, a backslash followed by an 'n' will result in a line-break at that point of the string. See the link for other examples. When a backslash is followed by a character which has no special formatting, it will leave just the second character and the backslash will not remain "\/" => "/"

What you want is double backslash "\", the first backslash will be seen as an escape and the second will signify that you want to keep the backslash in the final string.

https:\\/\\/www.example.com

pmar
  • 314
  • 1
  • 6
0

The value doesn't include a \ character as data in the first place. That is an escape character.

In JSON a / may be preceded by an escape character, but it doesn't need to be.

So in JSON "\/" and "/" are exactly equivalent.

In a URL, they are not equivalent.


If you want to include a \ in the URL (which you shouldn't, because it would make it an invalid URL) then you would need to escape the \ in the original JSON:

{
  "name": "url",
  "value": "https:\\/\\/www.example.com"
}

You could continue to escape the / too, but as mentioned above, this is pointless:

{
  "name": "url",
  "value": "https:\\\/\\\/www.example.com"
}
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I understand the escapes, but this becomes complicated. Since I don't want to touch the data, keep it original, there will be no pre-convert job to do on source json file, it may hurt to other values. I just want to read the value and get it output as same. Same as the different of `grep` and `fgrep` in bash scripting. Are there any exist functions I can use or do in nodejs directly? – BMW Jan 25 '19 at 23:29
  • @BMW — If you read an object from JSON, extract a property's value, then write the value back out by itself, it doesn't make sense to have it "the same". It wouldn't have the same meaning in plain text as the original did in JSON and it wouldn't parse as JSON. – Quentin Jan 26 '19 at 20:55