Inserting a JSON string (as opposed to DBObject) from Java

1,453 views
Skip to first unread message

wfc

unread,
Apr 6, 2009, 9:26:09 AM4/6/09
to mongodb-user
Hi

I am using the Java API to work with Mongo. I have a case where I
receive JSON as a Java string (it is already a valis JSON string), and
want to know if it is possible to just insert the string into a
collection, without having to map it to a BasicDBObject first? I have
looked around the API and didn't find anything that indicated such
usage.

Many thanks

Willie

Eliot Horowitz

unread,
Apr 6, 2009, 9:33:12 AM4/6/09
to mongod...@googlegroups.com, mongodb-user
There is no method righ now. We should add one to com.mongodb.util.JSON.

wfc

unread,
Apr 6, 2009, 11:17:38 AM4/6/09
to mongodb-user
Hi

Thanks for the reply

Would one create a method that first transforms the JSON string into a
DBObject, or is there (an existing) lower level Mongo method/utility
that one can use that already knows how to interpret JSON string (to
skip the transformation step) - for example, something that is
currently perhaps used in the jsonimport code? (I haven't gone and
looked at the jsonimport code yet)


On Apr 6, 3:33 pm, Eliot Horowitz <eliothorow...@gmail.com> wrote:
> There is no method righ now. We should add one to com.mongodb.util.JSON.
>

Eliot

unread,
Apr 6, 2009, 12:02:53 PM4/6/09
to mongod...@googlegroups.com
You would call JSON.parse() first, then save it to the DB.

The jsonimport code parses the json in the c++ client.

Kristina Chodorow

unread,
Apr 8, 2009, 10:49:51 AM4/8/09
to mongod...@googlegroups.com
I've added JSON.parse() to the Java driver, so if you pull from http://github.com/mongodb/mongo-java-driver/tree/master, you should be able to use it.

For example:

import com.mongodb.util.JSON;
/* ... */
DBObject obj = JSON.parse("{\"json\" : \"string\"}");
Reply all
Reply to author
Forward
0 new messages