"String not valid UTF-8” error when inserting json into mongodb

392 views
Skip to first unread message

Ragavan Ambighananthan

unread,
Jul 30, 2014, 12:01:03 PM7/30/14
to mongod...@googlegroups.com

i am storing a json file produced by cucumber json report into mongodb. But i get "String not valid UTF-8” when inserting json into mongodb.

Because the feature file has 年/月/日 , this is represented as "\u5e74/\u6708/\u65e5" in the json file. This is what i am doing to store it in mongo

json_string=File.read(file_path)
data = JSON.parse(json_string)
@col.insert(data)

I can see that after JSON.parse, the already encoded string further changes to "\x90\u0013s/\x90\u0013s/\x90\u0013s"

exception is happening at insert statement. any help appreciated.

I tried the following but still does not work

json_string=File.read(file_path,:encoding => 'UTF-8')
data = JSON.parse(json_string.force_encoding("UTF-8"))

Using Ruby 1.9.3

mongo (1.9.2)

any help would be useful

Regards

Ragavan

Asya Kamsky

unread,
Aug 1, 2014, 5:58:00 PM8/1/14
to mongodb-user
I'm not a Ruby expert (understatement of the year) but can you try something like this:

json_string = File.read(<etc>).to_json.dup.encode("UTF-8")
data = JSON.parse(json_string).with_indifferent_access



--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/baed8a8d-61aa-407e-b7a3-a4d789c480ce%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gary Murakami

unread,
Aug 18, 2014, 11:06:02 AM8/18/14
to mongod...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages