translate bson -> json on the fly

802 views
Skip to first unread message

Jordan Orelli

unread,
Apr 6, 2012, 3:11:39 PM4/6/12
to mgo-...@googlegroups.com
is it possible to translate bson into json on the fly?  I inherited a database that isn't modelled the way that I would model it, but there's also a client that already uses the json representation of these documents.  I'm going to change their structure, but as a stop-gap I want to simply expose some bson documents as json, since that's basically all the existing system was doing (without processing the data).  Anybody have any idea how I could translate a bson doc into json without knowing its structure in advance?

Jordan Orelli

unread,
Apr 6, 2012, 3:15:00 PM4/6/12
to mgo-...@googlegroups.com
err, I guess I should clarify:  I want to take the result of a query and turn it into json.  Iter.Next is internally unmarhasling the data, but I don't actually need to unmarshal the returned documents into a struct; I just want to print the result to json.

Gustavo Niemeyer

unread,
Apr 7, 2012, 10:25:51 AM4/7/12
to mgo-...@googlegroups.com
Hey Jordan,

On Fri, Apr 6, 2012 at 16:15, Jordan Orelli <jordan...@gmail.com> wrote:
> err, I guess I should clarify:  I want to take the result of a query and
> turn it into json.  Iter.Next is internally unmarhasling the data, but I
> don't actually need to unmarshal the returned documents into a struct; I
> just want to print the result to json.

Yeah, that's doable, and simple. Just using a map to load the document
from your query result, and then marshal the same map to json.

var m bson.M
err := query.One(&m)
... marshal m onto json ....

--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/plus
http://niemeyer.net/twitter
http://niemeyer.net/blog

-- I'm not absolutely sure of anything.

Erik Unger

unread,
Jul 29, 2012, 5:23:01 AM7/29/12
to mgo-...@googlegroups.com
I would be quite convenient to have that functionality in mgo/bson (also more efficient if implemented directly).

Since JSON is the natural text representation of BSON, what about allowing *string as destination for BSON unmarshalling?
bson.Raw.String() would also look quite natural and be handy for debugging.

-Erik

Gustavo Niemeyer

unread,
Jul 30, 2012, 1:10:38 AM7/30/12
to mgo-...@googlegroups.com
Hi Erik,

On Sun, Jul 29, 2012 at 6:23 AM, Erik Unger <ung...@gmail.com> wrote:
> I would be quite convenient to have that functionality in mgo/bson (also
> more efficient if implemented directly).

As I explained, converting to a map and then to json is trivial to do
with normal code, if that's what you're interested in doing.

> Since JSON is the natural text representation of BSON, what about allowing
> *string as destination for BSON unmarshalling?

It is actually not a proper text representation for BSON. MongoDB uses
several custom types to be able to represent the spectrum of BSON
typing in Javascript.

> bson.Raw.String() would also look quite natural and be handy for debugging.

If you want to see its content, just unmarshal it in a map and print the map:

func debugRaw(raw bson.Raw) {
var m map[string]interface{}
if err := raw.Unmarshal(&m); err != nil {
panic(err)
}
fmt.Printf("%#v\n", m)
}

>
> -Erik



--

gustavo @ http://niemeyer.net
Reply all
Reply to author
Forward
0 new messages