Malformed Json for an object with a rawfield only using XContentBuilder

(Andrea Zinicola) #1


I couldn't find a way to create a valid document using the XContentBuilder
including a raw field only.

Following the code I used:

  jsonStr = "{\"field\":\"value\"}";
  XContentBuilder xb = jsonBuilder()
      .rawField("object_name", jsonStr.getBytes())

This code resulted in a malformed json document { , "object_name" :
{"field":"value"} }. Looking at the JsonXContentGenerator implementation of
writeRawField it looks like this is what the code is expected to do, in

public void writeRawField(String fieldName, byte[] content, 

OutputStream bos) throws IOException {
generator.writeRaw(", "");
generator.writeRaw("" : ");

In order to overcome this I had to add a dummy empty field to the document:

  XContentBuilder xb = jsonBuilder()
      .field("dummy", "")
      .rawField(DOC_OBJECT_NAME, jsonStr.getBytes())

I wonder if there is any other method allowing to add a raw field only to a
document using XContentBuilder or not.

Thank you in advance,


You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit
For more options, visit

(system) #2