Search code examples
androidjsonexceptioncouchbasecouchbase-lite

Couchbaselite-Exception: Bad or missing JSON


I try to save properties in a Couchbase-document in Android. The properties hold a JSONArray with several JSONObjects.

When I do document.putproperties(myproperties) a couchbaseliteexception with state 400 and the message "bad or missing json" is thrown".

So the JSONArray looks like:

"some_prop" -> "[
{
    "content":"someContent",
    "key2":"",
    "key3":"",
    "key4":"",
    "month":8,
    "day":3,
    "key5":115
},
{
    "content":"Some other content",
    "key2":"something",
    "key3":"",
    "key4":"",
    "month":8,
    "day":3,
    "key5":115
}]"

Can anyone tell me whats the problem with this JSON?

EDIT: the JSONArray with the corresponding key is saved in a hashmap like it is explained in: http://developer.couchbase.com/mobile/develop/guides/couchbase-lite/native-api/document/index.html

EDIT 2: The Method where the update is executed and the JSONArray is filled:

private void updateDoc(ArrayList<MyObject> objects) {
    Document document = getDocument();

    // Update the document with more data
    Map<String, Object> updatedProperties = new HashMap<>();

    JSONArray objectArray = new JSONArray();

    //fill array with data
    for(MyObject element : objects) {
        JSONObject jsonObjects = element.toJSONObject();
        if(jsonObjects != null) {
            objectArray.put(jsonObjects);
        }
    }

    //set data to property map
    updatedProperties.put(MYOBJECT_PROP_IDENTIFIER, objectArray);

    try {
        // Save properties to the Couchbase local Couchbase Lite DB
        document.putProperties(updatedProperties);
    } catch (CouchbaseLiteException e) {
    }
}

Solution

  • Finally I found a solution to the problem: I don't use JSONObject or JSONArray anymore but save my data in ArrayList and put each element directly into the database. so i don't have an Array with all the elements which but a lot of single elements direktly in the document. To accesss them later I save also the number of elements in the DB. Each element has the index as a prefix so it can be identified later on. That's not quite a nice way but it works..