TranscodingException when appending JSON array to document

java

#1

I’m trying to update a document by adding an array to an existing array (in the future I might want to change it so the array gets created with a parent if the parent doesn’t exist). I first create a JSON array and then try to mutate the document like so:

JSONObject contents = new JSONObject();
try {
contents.put(“identifier”, “physical_deletion”);
} catch (JSONException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
contents.put(“status”, “accepted”);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
contents.put(“affected_documents”, counter);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
contents.put(“retries”, “?”);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

				JSONArray lastExecution = new JSONArray();
				lastExecution.put(contents);

DocumentFragment<Mutation> docUpdate = bucket
						.mutateIn("deletionProcess")
						.arrayAppend("LAST_EXECUTIONS", lastExecution)
						.execute();

But when I try to execute I get the following error:

com.couchbase.client.java.error.TranscodingException: Couldn’t encode subdoc fragment deletionProcess/LAST_EXECUTIONS “[{“identifier”:“physical_deletion”,“retries”:”?",“status”:“accepted”,“affected_documents”:11}]"

I can’t find anything online about this exception and don’t know what the cause is. Any help would be much appreciated


#2

Hi @boris.mtdv, the Couchbase JSON classes are called JsonObject, JsonArray etc. rather than JSONObject, JSONObject. Is it possible you’re pulling some other JSON library?


#3

Thanks a lot graham! Yes it turns out I was using the wrong library. I foolishly thought JSON was a universal format