# Create index with JSON XContentBuilder get a error

**URL:** <https://discuss.elastic.co/t/create-index-with-json-xcontentbuilder-get-a-error/185945>\
**Category:** Elasticsearch\
**Created:** [June 15, 2019, 3:18pm UTC](https://discuss.elastic.co/t/create-index-with-json-xcontentbuilder-get-a-error/185945 "2019-06-15T15:18:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hubo3085632](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hubo3085632/32/23724_2.png) [@hubo3085632](https://discuss.elastic.co/u/hubo3085632)\
**Post date:** [June 15, 2019, 3:18pm UTC](https://discuss.elastic.co/t/create-index-with-json-xcontentbuilder-get-a-error/185945/1 "2019-06-15T15:18:23Z")

</div>

Hi all , i met a weird issue , when i use JSON type XContentBuilder to create index's mapping ,it always return "Failed to close the XContentBuilder Caused by: java.io.IOException: Unclosed object or array found  
at org.elasticsearch.common.xcontent.json.JsonXContentGenerator.close(JsonXContentGenerator.java:469)  
at org.elasticsearch.common.xcontent.XContentBuilder.close(XContentBuilder.java:1000)  
... 36 common frames omitted".

This is the code :

private XContentBuilder getMappingBuilder() throws IOException {  
XContentBuilder builder = XContentFactory.jsonBuilder()  
.startObject()  
.startObject("properties")  
.startObject("@timestamp").field("type", "date")  
.startObject("timestamp").field("format", Constants.DateFormatSuper).field("type", "date")  
.endObject()  
.startObject("nation")  
.field("type", "keyword")  
.endObject()  
.startObject("app")  
.field("type", "keyword")  
.endObject()  
.startObject("order\_no")  
.field("type", "keyword")  
.endObject()  
.startObject("loan\_type")  
.field("type", "keyword")  
.endObject()  
.startObject("loan\_scenario")  
.field("type", "keyword")  
.endObject()  
.startObject("product\_type")  
.field("type", "keyword")  
.endObject()  
.startObject("installments").field("type", "integer").endObject()  
.startObject("platform")  
.field("type", "keyword")  
.endObject()  
.startObject("amount").field("type", "double").endObject()  
.startObject("latitude").field("type", "double").endObject()  
.startObject("longitude").field("type", "double").endObject()  
.startObject("location")  
.field("type", "text")  
.startObject("fields")  
.startObject("raw")  
.field("type", "keyword")  
.endObject()  
.endObject()  
.endObject()  
.startObject("geohash")  
.field("type", "geo\_point")  
.endObject()  
.startObject("city\_geohash")  
.field("type", "geo\_point")  
.endObject()  
.endObject()  
.endObject();  
return builder;  
}

restHighLevelClient.indices().create(new CreateIndexRequest(indexName).mapping(getMappingBuilder())  
.settings(Settings.builder()  
.put("index.number\_of\_shards", shards)  
.put("index.number\_of\_replicas", replicas))  
, RequestOptions.DEFAULT);

---

<div class="post-metadata">

**Author:** ![wangqinghuan](https://avatars.discourse-cdn.com/v4/letter/w/d26b3c/32.png) [@wangqinghuan](https://discuss.elastic.co/u/wangqinghuan)\
**Post date:** [June 15, 2019, 4:10pm UTC](https://discuss.elastic.co/t/create-index-with-json-xcontentbuilder-get-a-error/185945/2 "2019-06-15T16:10:27Z")

</div>

You have 20 startObject() and 19 endObject(), which is probably the reason that "IOException: Unclosed object ".

---

<div class="post-metadata">

**Author:** ![hubo3085632](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hubo3085632/32/23724_2.png) [@hubo3085632](https://discuss.elastic.co/u/hubo3085632)\
**Post date:** [June 17, 2019, 2:43am UTC](https://discuss.elastic.co/t/create-index-with-json-xcontentbuilder-get-a-error/185945/3 "2019-06-17T02:43:07Z")

</div>

果然是这个问题,非常感谢

---

<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 15, 2019, 2:52am UTC](https://discuss.elastic.co/t/create-index-with-json-xcontentbuilder-get-a-error/185945/4 "2019-07-15T02:52:02Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
