Catia Matos
Catia Matos

Reputation: 1

Split string by "\" Node.js

Hy everyone, I'm having some issues with split a string because when I log her value seems ok but in the final result appears "\" like,

"map":"{\"isRoot\": true, \"visible\": true}" instead of have "map":"{"isRoot": true, "visible": true}"

So I have this code

if(mapData!=undefined){
    var map = mapData.map;
    console.log("sendDeviceInfo getServicesMapInfoById map  >>>>>>>>>>>>>>>>>>>>> ", map);
    sendData.map=map;
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'sendDeviceInfo','sendData.map 1', sendData.map);
}

And my logs:

sendDeviceInfo getServicesMapInfoById map  >>>>>>>>>>>>>>>>>>>>>  {"isRoot": true, "visible": true}
4|wscontro | [2017-05-30 12:36:03.010] - debug: /opt/wscontroller/wscontroller-service/scripts/gps GpsController 58a8c61b-f11d-4874-91df-3a5205e4145f sendDeviceInfo sendData.map 1 "{\"isRoot\": true, \"visible\": true}"

Why is this happening?

--- solution

if(mapData!=undefined){
    var map = mapData.map;
    var aux = map.split('\\').join('');
    var jsonObject = JSON.parse(aux);
    sendData.map = jsonObject;  
}

Upvotes: 0

Views: 2037

Answers (4)

Glubus
Glubus

Reputation: 2855

Your Json string is using "-marks. Since Json is a string itself, we need a solution to tell the compiler which "-marks are marking the string, and which "-marks are part of the string itself.

To do this it's very common among languages to use the -character, and by typing for instance \", you're 'escaping' the "-mark.

Imagine a string like this: He said to me: "Hi there". Now if we want to make this into a string we could try "He said to me: "Hi there".", though the compiler will see 2 strings and a lost period, because it does not distinguish between the start and end of the string, and the "-marks that are part of the content itself.

When you console.log() an actual string, it will not show the "-marks that show the start and end of the string, because it is not necessary. Because of that, there is no need to escape the "-marks in the string itself.

Whatever the createLog() function does, it apparently needs to note the string down as it being an actual string, and therefor it needs to escape the "-marks with a '\' sign.

Upvotes: 0

sriam980980
sriam980980

Reputation: 1988

You can replace them like this

yourJsonString = yourJsonString.split('\\').join('');
var jsonObject = JSON.parse(yourJsonString);

Upvotes: 1

Rakesh Burbure
Rakesh Burbure

Reputation: 1045

Try using below code to remove the escape sequence

mapData = mapData.replace(/\\"/g, '"');

Upvotes: 0

ardilgulez
ardilgulez

Reputation: 1944

That's nothing to worry about. That "\"s don't actually exist in the string.

When you use JSON.stringify in node.js, the result always has "\"s to escape special characters, and double quotes are special characters. That character is the unix escape character. That's why it appears kind of everywhere.

Upvotes: 0

Related Questions