# Malformed Json for an object with a rawfield only using XContentBuilder

**URL:** <https://discuss.elastic.co/t/malformed-json-for-an-object-with-a-rawfield-only-using-xcontentbuilder/15009>\
**Category:** Elasticsearch\
**Created:** [December 24, 2013, 9:16am UTC](https://discuss.elastic.co/t/malformed-json-for-an-object-with-a-rawfield-only-using-xcontentbuilder/15009 "2013-12-24T09:16:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrea\_Zinicola](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andrea\_Zinicola](https://discuss.elastic.co/u/Andrea_Zinicola)\
**Post date:** [December 24, 2013, 9:16am UTC](https://discuss.elastic.co/t/malformed-json-for-an-object-with-a-rawfield-only-using-xcontentbuilder/15009/1 "2013-12-24T09:16:59Z")

</div>

Hi,

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()
      .startObject()
      .rawField("object_name", jsonStr.getBytes())
   .endObject();

```

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  
fact:

```
@Override
public void writeRawField(String fieldName, byte[] content, 

```

OutputStream bos) throws IOException {  
generator.writeRaw(", "");  
generator.writeRaw(fieldName);  
generator.writeRaw("" : ");  
flush();  
bos.write(content);  
}

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

```
  XContentBuilder xb = jsonBuilder()
      .startObject()
      .field("dummy", "")
      .rawField(DOC_OBJECT_NAME, jsonStr.getBytes())
   .endObject();

```

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,

Andrea

--  
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 [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/1eb49552-df19-4548-9696-534c1c3428b7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1eb49552-df19-4548-9696-534c1c3428b7%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 1:59am UTC](https://discuss.elastic.co/t/malformed-json-for-an-object-with-a-rawfield-only-using-xcontentbuilder/15009/2 "2017-07-06T01:59:32Z")

</div>


