Arrays in mapping throws exception

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.
For more options, visit https://groups.google.com/groups/opt_out.