JSON.parse() undesired behavior

255 views
Skip to first unread message

Weian

unread,
Dec 1, 2011, 6:13:26 PM12/1/11
to mongodb-user
Given the following JSON payload

{“name”:”c:\fakepath\event.xml”}

After using JSON.parse, the returned JSON object has value for "name"
as “c:fakpathevent.xml”. This is correct because the strings in JSON
payload are Javascript string and "\" is the escape character that
needs to be unescaped.

However, given another JSON payload

{“name”:”c:\\fakepath\\event.xml”}

It is expected that the resulting JSON object has "c:\fakepath
\event.xml" as value for "name". But JSONparse() returns something
different "c:\\fakepath\\event.xml".

This smells like a bug.

Eliot Horowitz

unread,
Dec 3, 2011, 12:52:34 AM12/3/11
to mongod...@googlegroups.com
Is this java?
Can you file the full code in a ticket @ http://jira.mongodb.org/

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>

JJO

unread,
Dec 5, 2011, 10:38:38 AM12/5/11
to mongodb-user
Doesn't it depend on where you are looking at the string? I mean, it's
possible, depending on where you are looking at it, that it may be re-
escaping for your benefit. This is what I get in the JS shell, for
example.

> db.fakepath.save({"name":"c:\fakepath\event.xml"})
> db.fakepath.save({"name":"c:\\fakepath\\event.xml"})
> db.fakepath.find()
{ "_id" : ObjectId("4edce3eb0912f9e15972c252"), "name" : "c:
\fakepathevent.xml" }
{ "_id" : ObjectId("4edce3f60912f9e15972c253"), "name" : "c:\\fakepath\
\event.xml" }
> db.fakepath.find().forEach(function(doc) { print(doc.name.length); })1921

- this indicates to me that \f is one character, and \\ is also
treated as one character.

Chris Westin

unread,
Dec 5, 2011, 4:40:53 PM12/5/11
to mongodb-user
JJO, that's definitely wrong. I've filed https://jira.mongodb.org/browse/SERVER-4431
for the issue in the mongo shell.

Weian, that looks wrong, but we need to know the driver language and
see some more code sample to be sure. I checked, and it doesn't look
like a ticket has been filed for the issue. Can you please do so as
Eliot suggested, or else get us a code fragment here?

FYI, JavaScript escape sequences can be looked up here:
http://www.javascriptkit.com/jsref/escapesequence.shtml .

Weian

unread,
Dec 6, 2011, 12:37:17 PM12/6/11
to mongodb-user
Hi, JJO,

u r right. The toString() of mongo's json object using
JSON.Serialize() and gets the escaped payload. That's not the in
memory java string.
It causes confusion when you use debugger.

wd

Weian

unread,
Dec 6, 2011, 12:47:14 PM12/6/11
to mongodb-user
Hi, Crhis,

Thank you for filing the bug.

I am using Java, the code example is simple

String myJSONString = "{\"filename\":\"c:\\\\a\\\\d.txt\"}"'
JSONDBObject myObj = JSON.parse(payload);
System.out.println(myObj);

Weian


On 12月5日, 下午1时40分, Chris Westin <cwes...@yahoo.com> wrote:
> JJO, that's definitely wrong. I've filedhttps://jira.mongodb.org/browse/SERVER-4431

> > > This smells like a bug.- 隐藏被引用文字 -
>
> - 显示引用的文字 -

JJO

unread,
Dec 7, 2011, 10:44:18 AM12/7/11
to mongodb-user
Two things:
First, is the JSON class provided by the Java Mongo driver? That
seems like it might be something in the JRE.

Second, I think you are overescaping the backslashes. I think you
really just want this:

String myJSONString = "{\"filename\":\"c:\\a\\d.txt\"}"' JSONDBObject
myObj = JSON.parse(payload); System.out.println(myObj);

Notice, in the first line, that the backslashes are only escaped once
instead of twice. For comparison, you are only escaping the double-
quotes once, so I believe you should escape your backslashes only once
as well.

JJO

Weian

unread,
Dec 14, 2011, 11:23:52 AM12/14/11
to mongodb-user
Hi, JJO,

As I mentioned in my other reply, the problem is mongo's JSON object.
It's toString() method used JSON.serialize().

As your second point, your understanding of java and JSON string
escaping is not correct. the java code you wrote

String myJSONString = "{\"filename\":\"c:\\a\\d.txt\"}"'

will results in the the following actual myJSOString in memory

{"filename" : "c:\a\d.txt"}

When you do a JSON.parse on this JSON string, the resulting JSON
object's filename attribute is (again in memory)

c:ad.txt

Which is not the desire result.

Try to write and run a test java program and see.

Regards!

Weian

On Dec 7, 7:44 am, JJO <j...@emberex.com> wrote:
> Two things:
> First, is the JSON class provided by the Java Mongo driver? That
> seems like it might be something in the JRE.
>
> Second, I think you are overescaping the backslashes. I think you
> really just want this:
>
> String myJSONString = "{\"filename\":\"c:\\a\\d.txt\"}"' JSONDBObject

> myObj =JSON.parse(payload); System.out.println(myObj);

> > > > > After usingJSON.parse, the returned JSON object has value for "name"

Reply all
Reply to author
Forward
0 new messages