json.Marshal() giving me invalid UTF-8 message

663 views
Skip to first unread message

Matt Parlane

unread,
Jun 4, 2011, 11:51:18 PM6/4/11
to golang-nuts
Hi there...

I'm completely new to Go, and my first project is something that grabs
a bunch of data from a MongoDB database and converts the objects to
JSON strings. I've got this so far:

for cursor.HasNext() {
var m mongo.M
err := cursor.Next(&m)
if err != nil { log.Fatal(err) }
log.Print(m)
jsonStr, err := json.Marshal(m)
if err != nil { log.Fatal(err) }
log.Print(jsonStr)
}

The program bails on the first time through this loop, and prints
this:

2011/06/05 15:34:59 map[top:37 name: height:120 width:186 Template2_ID:
4c4ea54697a5679a5cf8f61a Template2Sector_ID:4c4f9e45f6440c3da02894ed
left:414 _id:4c4f9e45148c00c4106e907e]
2011/06/05 15:34:59 json: invalid UTF-8 in string: "LO\x9eE\xf6D\f=
\xa0(\x94\xed"

The data is coming from MongoDB and it stores data as UTF-8 so I don't
see how it could be invalid. I've checked that exact record using
other MongoDB tools and it seems to be fine. If it helps, I'm using
this driver:

http://garyburd.github.com/go-mongo

Cheers,

Matt

Matt Parlane

unread,
Jun 4, 2011, 11:54:45 PM6/4/11
to golang-nuts
grr... my linebreaks got screwed up, of course... the output is two
lines, each one starting with the date/time.

Matt Parlane

unread,
Jun 5, 2011, 1:13:31 AM6/5/11
to golang-nuts
On Jun 5, 5:11 pm, gary.b...@gmail.com wrote:
> The JSON encoder is complaining about the _id element of the document.
>
> The _id element in the document above is a BSON object identifier.  A BSON
> object identifier is a binary 12 byte value.  Object identifiers are not
> valid UTF-8 strings.
>
> There are two ways to work around the problem:
>
> - If the _id element is not needed in the JSON output, then delete the _id
> element before encoding the document:  m["_id"] = nil, false
>
> - If the _id element is needed in the JSON output, then you will need to
> convert the element to text before encoding the _id to JSON.  You can use
> the object id String method
> (http://garyburd.github.com/go-mongo/pkg/mongo.html#ObjectId.String) to
> convert the object id to a hex string and NewObjectIdHex
> (http://garyburd.github.com/go-mongo/pkg/mongo.html#NewObjectIdHex) to
> convert the hex string back to an object id.

From the man himself!

Thanks so much, I'll give that a go.

Matt
Reply all
Reply to author
Forward
Message has been deleted
0 new messages