# How to write json array to elasticsearch's objetct field by es rest client?

**URL:** <https://discuss.elastic.co/t/how-to-write-json-array-to-elasticsearchs-objetct-field-by-es-rest-client/255224>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [November 12, 2020, 2:24pm UTC](https://discuss.elastic.co/t/how-to-write-json-array-to-elasticsearchs-objetct-field-by-es-rest-client/255224 "2020-11-12T14:24:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![KaimingWan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kaimingwan/32/78909_2.png) [@KaimingWan](https://discuss.elastic.co/u/KaimingWan)\
**Post date:** [November 12, 2020, 2:24pm UTC](https://discuss.elastic.co/t/how-to-write-json-array-to-elasticsearchs-objetct-field-by-es-rest-client/255224/1 "2020-11-12T14:24:03Z")

</div>

I use elasticsearch6.8 and jdk8. The key problem is hot to write json array string to elasticsearch's object type field. Following code show how I tried to write to es.

My mapping is as follows:  
`{ "mapping": { "_doc": { "properties": { "c_json": { "type": "object" } } } } }`

My c\_json value is :

`{ "key1": "val1", "key2": "val2", "key3": "val3" }`

My java code is :

```auto
String json="{
  "key1": "val1",
  "key2": "val2",
  "key3": "val3"
}";
String keyName="c_json";
Map<String,String> myDataMap = new HashMap();
myDataMap.put(keyName,json);

IndexRequest indexRequest = new IndexRequest("my_index").id(esIdValue)
                        .type("_doc")
                        .source(XContentType.JSON,convertMapToEsValues(myDataMap));
           BulkRequest bulkRequest = new BulkRequest();
            BulkResponse bulkResponse = es6Client.bulk(bulkRequest, RequestOptions.DEFAULT);

    public static Object[] convertMapToEsValues(Map<String, Object> valueMap) {
        List<Object> kvList = Lists.newArrayList();
        for (String fieldName : valueMap.keySet()) {
            kvList.add(fieldName);
            kvList.add(valueMap.get(fieldName));
        }
        return kvList.toArray();
    }

```

When I run the code, es server give me the exception:  
` ElasticsearchException[Elasticsearch exception [type=mapper_parsing_exception, reason=object mapping for [c_json] tried to parse field [c_json] as object, but found a concrete value]],action id is 9, Index name is my_index`

---

<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:** [December 10, 2020, 2:24pm UTC](https://discuss.elastic.co/t/how-to-write-json-array-to-elasticsearchs-objetct-field-by-es-rest-client/255224/2 "2020-12-10T14:24:18Z")

</div>

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