# Arrays in mapping throws exception

**URL:** <https://discuss.elastic.co/t/arrays-in-mapping-throws-exception/11498>\
**Category:** Elasticsearch\
**Created:** [April 9, 2013, 9:51am UTC](https://discuss.elastic.co/t/arrays-in-mapping-throws-exception/11498 "2013-04-09T09:51:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chetana](https://avatars.discourse-cdn.com/v4/letter/c/dbc845/32.png) [@Chetana](https://discuss.elastic.co/u/Chetana)\
**Post date:** [April 9, 2013, 9:51am UTC](https://discuss.elastic.co/t/arrays-in-mapping-throws-exception/11498/1 "2013-04-09T09:51:28Z")

</div>

I am trying to create a mapping by reading a Json keys (string). Inner  
object, properties work fine but when I try for array, I am getting \*  
org.elasticsearch.common.jackson.core.JsonGenerationException\*: Can not  
write a field name, expecting a value

My code looks like this  
writeFields(JsonNode node,XContentBuilder xbMapping) {  
while(fieldItr.hasNext()) {  
Entry\< String, JsonNode\> keyValueEntry = fieldItr.next();  
if(keyValueEntry.getValue().isObject()) {

```
 if(keyValueEntry.getKey()!= null && !"".equals(keyValueEntry.getKey()))
  xbMapping.startObject(keyValueEntry.getKey());
  
 else
  xbMapping.startObject();
 xbMapping.startObject("properties");
 writeFields(keyValueEntry.getValue(), xbMapping);
 xbMapping.endObject();
 xbMapping.endObject();
} else if(keyValueEntry.getValue().isArray()) {
 if(keyValueEntry.getKey()!= null && !"".equals(keyValueEntry.getKey()))
  xbMapping.startArray(keyValueEntry.getKey());
 else 
  xbMapping.startArray();
   
 Iterator<JsonNode> arrJsonItr = keyValueEntry.getValue().elements();
 if(arrJsonItr.hasNext()) {
  writeFields(arrJsonItr.next(), xbMapping);
 }
 
 xbMapping.endArray();
 
} else if(!keyValueEntry.getValue().isArray() && 

```

!keyValueEntry.getValue().isObject()){

```
 if(keyValueEntry.getKey()!= null && !"".equals(keyValueEntry.getKey()))
  xbMapping.startObject(keyValueEntry.getKey());
  
 else
  xbMapping.startObject()
 
 .field("type", "string");

  xbMapping.field("index", "analyzed");

  xbMapping.endObject();

```

For me, mapping looks fine. Can someone please figure out the problem?

--  
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).  
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, 2:42am UTC](https://discuss.elastic.co/t/arrays-in-mapping-throws-exception/11498/2 "2017-07-06T02:42:06Z")

</div>


