CouchDB and Special Characters

Tag: couchdb , special-characters Author: bosswei007 Date: 2009-12-25

Question...I am trying to add data to CouchDB via the _bulk_docs API. For the most part I have this working, but any JSON value that contains a special character (e.g. certain Spanish or French characters exist in my data) results in an "invalid JSON" error given by CouchDB, and the data is rejected.

I'm not sure what I need to set in order to get this to work. Any suggestions? CouchDB seems to get the data properly (at least it looks correct in its log file), but it is not processed unless I remove the special chars.

CouchDB supports Unicode characters so there should be no problem with special characters. What library/tool are you using to talk to CouchDB? The problem might be there.
Right now I am invoking the URL from a Java servlet. I create a URL object which points to my CouchDB database and set the Content-type of my HttpURLConnection to "text/plain;charset=utf-8". I also use Jackson (using the writeValueAsString() method) to create the JSON string that is sent to CouchDB.

Other Answer1

content-type should be application/json, not test/plain

I'm not familiar with your JSON parser but make sure it's utf-8, just because your http client library is set to utf-8 doesn't mean your json serializer is outputing unicode.

Other Answer2

I'd try to debug the problem doing something like this:

  1. install on your machine a packet sniffer that understands http (e.g. wireshark) and launch it capturing packages sent to port 5984
  2. Create documents using curl from the command line:

    curl -X POST [email protected] http://localhost:6984/testdb/_bulk_docs

    where test.js contains some documents with special characters. And see if this works as expected.

  3. do the same with the java library and see the differences in http headers and request body in the 2 cases.

NOTE: curl by default should send the data with content-type: application/x-www-form-urlencoded and this might be the problem, but check the headers because I'm not sure.

Hope this can help you.


Thanks! This was all very is now working. JQuery-based client web app to/from Jetty/Cometd, CouchDB, and Jackson are all talking :)