# Failed to close the XContentBuilder

**URL:** <https://discuss.elastic.co/t/failed-to-close-the-xcontentbuilder/175492>\
**Category:** Elasticsearch\
**Created:** [April 4, 2019, 11:38pm UTC](https://discuss.elastic.co/t/failed-to-close-the-xcontentbuilder/175492 "2019-04-04T23:38:55Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![EliuFlorez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eliuflorez/32/40884_2.png) [@EliuFlorez](https://discuss.elastic.co/u/EliuFlorez)\
**Post date:** [April 4, 2019, 11:38pm UTC](https://discuss.elastic.co/t/failed-to-close-the-xcontentbuilder/175492/1 "2019-04-04T23:38:55Z")

</div>

Hi guys, I'm presenting an error to install the plugin or make a query.

Code:

```auto
@Override
    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    	builder.startObject(simpleName());
        builder.field("type", CONTENT_TYPE);
        startDateMapper.toXContent(builder, params);
        endDateMapper.toXContent(builder, params);
        rruleMapper.toXContent(builder, params);
        multiFields.toXContent(builder, params);
        builder.endObject();
        return builder;
    }

```

Error:

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "parse_exception",
        "reason": "Failed to parse content to map",
        "suppressed": [
          {
            "type": "illegal_state_exception",
            "reason": "Failed to close the XContentBuilder"
          }
        ]
      }
    ],
    "type": "parse_exception",
    "reason": "Failed to parse content to map",
    "caused_by": {
      "type": "json_parse_exception",
      "reason": "Duplicate field 'recurrent_date'\n at [Source: org.elasticsearch.common.compress.DeflateCompressor$1@11d6fb74; line: 1, column: 102]"
    },
    "suppressed": [
      {
        "type": "illegal_state_exception",
        "reason": "Failed to close the XContentBuilder",
        "caused_by": {
          "type": "i_o_exception",
          "reason": "Unclosed object or array found"
        }
      }
    ]
  },
  "status": 400
}

```

Do you have any idea why?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 5, 2019, 12:55am UTC](https://discuss.elastic.co/t/failed-to-close-the-xcontentbuilder/175492/2 "2019-04-05T00:55:27Z")

</div>

A guess based on:

```
Duplicate field 'recurrent_date'

```

You have a duplicated field?

---

<div class="post-metadata">

**Author:** ![EliuFlorez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eliuflorez/32/40884_2.png) [@EliuFlorez](https://discuss.elastic.co/u/EliuFlorez)\
**Post date:** [April 5, 2019, 1:23pm UTC](https://discuss.elastic.co/t/failed-to-close-the-xcontentbuilder/175492/3 "2019-04-05T13:23:33Z")

</div>

Hello, in my map I only have a recurring\_date field specified. and this is the error that I have.

if you can give a view to my code. : D  
link: [https://github.com/EliuFlorez/recurring-plugin](https://github.com/EliuFlorez/recurring-plugin)

JSON:

```auto
PUT example_event
{
  "settings": {
    "index": {
      "number_of_shards": 1,
      "number_of_replicas": 0
    },
    "analysis": {
      "analyzer": {
        "std_lang": {
          "type": "standard",
          "stopwords": "_english_"
        }
      }
    }
  },
  "mappings": {
    "event": {
      "properties": {
        "name": {
          "type": "text",
          "analyzer": "std_lang"
        },
        "recurrent_date": {
          "type": "recurring"
        }
      }
    }
  }
}

```

Error:

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "generation_exception",
        "reason": "failed to serialize source for type [event]"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "Failed to parse mapping [event]: failed to serialize source for type [event]",
    "caused_by": {
      "type": "generation_exception",
      "reason": "failed to serialize source for type [event]",
      "caused_by": {
        "type": "illegal_state_exception",
        "reason": "Failed to close the XContentBuilder",
        "caused_by": {
          "type": "i_o_exception",
          "reason": "Unclosed object or array found"
        },
        "suppressed": [
          {
            "type": "illegal_state_exception",
            "reason": "Failed to close the XContentBuilder",
            "caused_by": {
              "type": "i_o_exception",
              "reason": "Unclosed object or array found"
            }
          }
        ]
      }
    }
  },
  "status": 400
}

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 5, 2019, 1:38pm UTC](https://discuss.elastic.co/t/failed-to-close-the-xcontentbuilder/175492/4 "2019-04-05T13:38:35Z")

</div>

This is not the same error message that you originally pasted. I guess you changed things in the meantime.

Anyway here the problem is that the generated json is not correctly formed. You are missing something similar to a `}` or a `]`.

---

<div class="post-metadata">

**Author:** ![EliuFlorez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eliuflorez/32/40884_2.png) [@EliuFlorez](https://discuss.elastic.co/u/EliuFlorez)\
**Post date:** [April 5, 2019, 2:31pm UTC](https://discuss.elastic.co/t/failed-to-close-the-xcontentbuilder/175492/5 "2019-04-05T14:31:03Z")

</div>

Hello, in what way can I print the constructor to see so that the structure is generated and closes me.

I'm trying with:

```auto
throw new IllegalArgumentException("builder.toString:" + builder.toString());

```

but did not print the json string or structure created.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 5, 2019, 2:51pm UTC](https://discuss.elastic.co/t/failed-to-close-the-xcontentbuilder/175492/6 "2019-04-05T14:51:38Z")

</div>

I guess it can't because your XContentBuilder object is not properly closed.

---

<div class="post-metadata">

**Author:** ![EliuFlorez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eliuflorez/32/40884_2.png) [@EliuFlorez](https://discuss.elastic.co/u/EliuFlorez)\
**Post date:** [April 5, 2019, 3:03pm UTC](https://discuss.elastic.co/t/failed-to-close-the-xcontentbuilder/175492/7 "2019-04-05T15:03:59Z")

</div>

If I still have an error there is and it is so simple. and if I delete "public XContentBuilder toXContent ()" from my code, what difference did it open?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 5, 2019, 3:38pm UTC](https://discuss.elastic.co/t/failed-to-close-the-xcontentbuilder/175492/8 "2019-04-05T15:38:59Z")

</div>

I don't understand the question.

---

<div class="post-metadata">

**Author:** ![EliuFlorez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eliuflorez/32/40884_2.png) [@EliuFlorez](https://discuss.elastic.co/u/EliuFlorez)\
**Post date:** [April 5, 2019, 4:00pm UTC](https://discuss.elastic.co/t/failed-to-close-the-xcontentbuilder/175492/9 "2019-04-05T16:00:58Z")

</div>

The question is that if you delete from my "toXContent"

```auto
startDateMapper.toXContent(builder, params);
        endDateMapper.toXContent(builder, params);
        rruleMapper.toXContent(builder, params);
        multiFields.toXContent(builder, params);

```

it would be like that:

```auto
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    	builder.startObject(simpleName());
        builder.field("type", CONTENT_TYPE);
        builder.endObject();
        return builder;
    }

```

it works to create the mapper. I do not know if it affects something else as in the results of the query that the plugin should return.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 5, 2019, 5:09pm UTC](https://discuss.elastic.co/t/failed-to-close-the-xcontentbuilder/175492/10 "2019-04-05T17:09:56Z")

</div>

I don't know.

---

<div class="post-metadata">

**Author:** ![EliuFlorez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eliuflorez/32/40884_2.png) [@EliuFlorez](https://discuss.elastic.co/u/EliuFlorez)\
**Post date:** [April 5, 2019, 5:10pm UTC](https://discuss.elastic.co/t/failed-to-close-the-xcontentbuilder/175492/11 "2019-04-05T17:10:44Z")

</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:** [May 3, 2019, 5:22pm UTC](https://discuss.elastic.co/t/failed-to-close-the-xcontentbuilder/175492/12 "2019-05-03T17:22:57Z")

</div>

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