# XContentBuilder Can not write a field name, expecting a value - what value?

**URL:** <https://discuss.elastic.co/t/xcontentbuilder-can-not-write-a-field-name-expecting-a-value-what-value/211796>\
**Category:** Elasticsearch\
**Created:** [December 13, 2019, 1:28pm UTC](https://discuss.elastic.co/t/xcontentbuilder-can-not-write-a-field-name-expecting-a-value-what-value/211796 "2019-12-13T13:28:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ishv](https://avatars.discourse-cdn.com/v4/letter/i/4da419/32.png) [@ishv](https://discuss.elastic.co/u/ishv)\
**Post date:** [December 13, 2019, 1:28pm UTC](https://discuss.elastic.co/t/xcontentbuilder-can-not-write-a-field-name-expecting-a-value-what-value/211796/1 "2019-12-13T13:28:54Z")

</div>

I'm trying to set up the following mapping structure in Java High-Lever Rest API:

```
{
    "mappings": {
        "dynamic_templates": [{
            "articleNumber": {
                {
                    "match_mapping_type": "*"
                },
                {
                    "match": "articleNumber*"
                },
                {
                    "mapping": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword"
                            }
                        }

                    }
                }

            }
        }]
    }
}

```

I decided to use XContentBuilder for that:

```
XContentBuilder mapping = XContentFactory.jsonBuilder();

mapping.startObject()
    .startObject("mappings")
        .startArray("dynamic_templates")
            .startObject("articleNumber")
                .field("match_mapping_type", "*")
                .field("match", "articleNumber*")
                .startObject("mapping")
                    .field("type", "text")
                    .startObject("fields")
                        .startObject("keyword")
                            .field("type", "keyword")
                        .endObject()
                    .endObject()
                .endObject()
            .endObject()
        .endArray()
    .endObject();
    
return mapping;

```

Everything due to the instructions, but the following error was received:

`com.fasterxml.jackson.core.JsonGenerationException: Can not write a field name, expecting a value`, and it points out to this line: `.startObject("articleNumber")`.

Am I creating the JSON object wrong? Or the mapping structure is invalid?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [December 16, 2019, 9:10am UTC](https://discuss.elastic.co/t/xcontentbuilder-can-not-write-a-field-name-expecting-a-value-what-value/211796/2 "2019-12-16T09:10:12Z")

</div>

I think you need to squeeze another `startObject` after `.startArray("dynamic_templates")` and `.startObject("articleNumber") in order to resemble the `{`after the`[`array starts.`

---

<div class="post-metadata">

**Author:** ![ishv](https://avatars.discourse-cdn.com/v4/letter/i/4da419/32.png) [@ishv](https://discuss.elastic.co/u/ishv)\
**Post date:** [December 16, 2019, 9:16am UTC](https://discuss.elastic.co/t/xcontentbuilder-can-not-write-a-field-name-expecting-a-value-what-value/211796/3 "2019-12-16T09:16:22Z")

</div>

Now, I'm receiving the different error:

`Invocation of i nit method failed; nested exception is ElasticsearchStatusException[Elasticsearch exception [type=mapper_parsing_exception, reason=Failed to parse mapping [_doc]: Root mapping definition has unsupported parameters: [mappings : {dynamic_templates=[{articleNumber={mapping={type=text, fields={keyword={type=keyword}}}, match_mapping_type=string, match=articleNumber*}}]}]]]; nested: ElasticsearchException[Elasticsearch exception [type=mapper_parsing_exception, reason=Root mapping definition has unsupported parameters: [mappings : {dynamic_templates=[{articleNumber={mapping={type=text, fields={keyword={type=keyword}}}, match_mapping_type=string, match=articleNumber*}}]}]]];`

Is it something with object parser?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [December 16, 2019, 9:27am UTC](https://discuss.elastic.co/t/xcontentbuilder-can-not-write-a-field-name-expecting-a-value-what-value/211796/4 "2019-12-16T09:27:53Z")

</div>

what endpoint are you calling? can you provide a full reproduction instead of just snippets?

---

<div class="post-metadata">

**Author:** ![ishv](https://avatars.discourse-cdn.com/v4/letter/i/4da419/32.png) [@ishv](https://discuss.elastic.co/u/ishv)\
**Post date:** [December 16, 2019, 11:04am UTC](https://discuss.elastic.co/t/xcontentbuilder-can-not-write-a-field-name-expecting-a-value-what-value/211796/5 "2019-12-16T11:04:46Z")

</div>

The problem was fixed by removing `mappings` object.  
This is the correct structure of a mapping body:

```
{
    "dynamic_templates": [
        {
            "articleNumber": {
                "match_mapping_type": "string",
                "match": "articleNumber*",
                "mapping": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword"
                        }
                    }
                    
                }
            }
        }
    ] 
}
```

---

<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:** [January 13, 2020, 11:04am UTC](https://discuss.elastic.co/t/xcontentbuilder-can-not-write-a-field-name-expecting-a-value-what-value/211796/6 "2020-01-13T11:04:51Z")

</div>

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