How to remove slashes in formatted JSON data in Mongo using Nodejs?

I store data in a mongo table in JSON format using Nodejs and mongoskin. Mongo automatically adds slashes to formatted json data. Then, when I retrieve, I cannot parse the data. Please suggest me how to remove slashes or how to perform data analysis?

db.messagetable.find().forEach(printjson) { "_id" : "1861574", "MatriId" : "1861574", "session" : "{\"messages\":{\"cometchat\":{\"timedifference\":0,\"cometchat_buddytime\":0,\"msgavails\":\"\"}}}", "expires" : 1341702134 } 
+4
source share
2 answers

here slahses are used to exit charatcer " into your JSON encoded string (not a json object, because

 > JSON.parse("{messages:1}") SyntaxError: Unexpected token m at Object.parse (native) at repl:1:7 at REPLServer.eval (repl.js:80:21) at Interface.<anonymous> (repl.js:182:12) at Interface.emit (events.js:67:17) at Interface._onLine (readline.js:162:10) at Interface._line (readline.js:426:8) at Interface._ttyWrite (readline.js:603:14) at ReadStream.<anonymous> (readline.js:82:12) at ReadStream.emit (events.js:88:20) 

therefore, the keys must be enclosed in " to represent the string, but you cannot just put " inside the string. To fix this json parsers add \

  > JSON.parse("{\"messages\":1}") { messages: 1 } 

so when you parse just call JSON.Parse in the session line

 > JSON.parse("{\"messages\":{\"cometchat\":{\"timedifference\":0,\"cometchat_buddytime\":0,\"msgavails\":\"\"}}}") { messages: { cometchat: { timedifference: 0, cometchat_buddytime: 0, msgavails: '' } } } 
+5
source

Slashes are not in the string. Chatter is used to display characters such as " because " used as a start / end delimiter for strings.

If you were a console.log session this object that you printed, it will be printed without a slash.

No slash.

+1
source

Source: https://habr.com/ru/post/1392417/


All Articles