# How to POST JSON array to Elastic Search

**URL:** <https://discuss.elastic.co/t/how-to-post-json-array-to-elastic-search/54851>\
**Category:** Elasticsearch\
**Created:** [July 6, 2016, 4:58pm UTC](https://discuss.elastic.co/t/how-to-post-json-array-to-elastic-search/54851 "2016-07-06T16:58:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vasumathy](https://avatars.discourse-cdn.com/v4/letter/v/2bfe46/32.png) [@vasumathy](https://discuss.elastic.co/u/vasumathy)\
**Post date:** [July 6, 2016, 4:58pm UTC](https://discuss.elastic.co/t/how-to-post-json-array-to-elastic-search/54851/1 "2016-07-06T16:58:47Z")

</div>

I am trying to do a curl call to post my JSON to Elastic search. My JSON file has the following data.

[  
{  
name1:val1,  
name2:val2  
},  
{  
name1:val3,  
name2:val4  
}  
]

Since the JSON starts with array I get a mapping exception when I try to POST The data to ElasticSearch.  
ERROR:  
{"error":{"root\_cause":[{"type":"mapper\_parsing\_exception","reason":"failed to parse"}],"type":"mapper\_parsing\_exception","reason":"failed to parse","caused\_by":{"type":"not\_x\_content\_exception","reason":"Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"}},"status":400}LM-SJC-11002568

If i change my JSON to the below format, I am able to post the data to elastic search.  
{  
"result":[  
{  
name1:val1,  
name2:val2  
},  
{  
name1:val3,  
name2:val4  
}  
]  
}  
But performing analysis on this JSON is not yielding correct results. Because it considers only the "result" while counting the number of documents and hence the "total" is always "1".

My ElasticSearch JSON:  
{  
"took":16,  
"timed\_out":false,  
"\_shards":{  
"total":5,  
"successful":5,  
"failed":0  
},  
"hits":{  
"total":1,  
"max\_score":1,  
"hits":[  
{  
"\_index":"testing",  
"\_type":"1",  
"\_id":"\*\*\*\*\*\*",  
"\_score":1,  
"\_source":{  
"result":[  
{  
name1:val1,  
name2:val2  
},  
{  
name1:val3,  
name2:val4  
}  
]  
}  
}

NOTE: Total is one though result has two documents inside it. I need a way to upload the Json Array directly into elasticSearch

---

<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:** [July 6, 2016, 5:33pm UTC](https://discuss.elastic.co/t/how-to-post-json-array-to-elastic-search/54851/2 "2016-07-06T17:33:13Z")

</div>

Please format your code.

A document like:

```auto
[
// Something
]

```

Is not a valid JSON document. You need to provide indeed a JSON document to elasticsearch.

Here you want to index:

```auto
{
name1 : val1,
name2 : val2
}

```

and

```auto
{
name1 : val3,
name2 : val4
}

```

As they are 2 different docs for your use case.

---

<div class="post-metadata">

**Author:** ![unknownunknown](https://avatars.discourse-cdn.com/v4/letter/u/4da419/32.png) [@unknownunknown](https://discuss.elastic.co/u/unknownunknown)\
**Post date:** [July 6, 2016, 6:23pm UTC](https://discuss.elastic.co/t/how-to-post-json-array-to-elastic-search/54851/3 "2016-07-06T18:23:11Z")

</div>

I find running JSON objects through 'jq(1)' an easy way to validate them for correctness. You could also use an online tool like sense or JSON lint (note that this might expose your data, so use test data).

---

<div class="post-metadata">

**Author:** ![vasumathy](https://avatars.discourse-cdn.com/v4/letter/v/2bfe46/32.png) [@vasumathy](https://discuss.elastic.co/u/vasumathy)\
**Post date:** [July 6, 2016, 9:19pm UTC](https://discuss.elastic.co/t/how-to-post-json-array-to-elastic-search/54851/4 "2016-07-06T21:19:30Z")

</div>

Thank you for your responses. I have a list of objects in python. I am using json.dumps(list\_obj) which gives me a JSON array. I want each array element of the array to be one document in elastic search. Please let me know if there is any way to do this.

---

<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:** [July 6, 2016, 11:08pm UTC](https://discuss.elastic.co/t/how-to-post-json-array-to-elastic-search/54851/5 "2016-07-06T23:08:06Z")

</div>

It's a Python question IMO.

---

<div class="post-metadata">

**Author:** ![unknownunknown](https://avatars.discourse-cdn.com/v4/letter/u/4da419/32.png) [@unknownunknown](https://discuss.elastic.co/u/unknownunknown)\
**Post date:** [July 13, 2016, 7:51pm UTC](https://discuss.elastic.co/t/how-to-post-json-array-to-elastic-search/54851/6 "2016-07-13T19:51:29Z")

</div>

> [@vasumathy](#):
>
> I have a list of objects in python. I am using json.dumps(list\_obj) which gives me a JSON array. I want each array element of the array to be one document in Elasticsearch. Please let me know if there is any way to do this.

I would think that you would hold off on turning your list into an object until you're ready to submit the document, and instead iterate over the list items, dump those, and send the dumped document to elasticsearch.

Pseudocode (where es.index is your favorite way to get the thing indexed; there are Python ES bindings...)

```
for x in list:
  obj = json.dumps(x)
  es.index(obj)

```

---

<div class="post-metadata">

**Author:** ![shivam\_rastogi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shivam_rastogi/32/47660_2.png) [@shivam\_rastogi](https://discuss.elastic.co/u/shivam_rastogi)\
**Post date:** [April 4, 2017, 10:02am UTC](https://discuss.elastic.co/t/how-to-post-json-array-to-elastic-search/54851/7 "2017-04-04T10:02:14Z")

</div>

Hi ,  
I am also facing the similar challenge.

The data I have looks similar to this -

`{"totalName":2,"names":[{"name":"name1"},{"name":"name2"}]}`

I understand the approach you proposed works, but **is it efficient to make separate REST call for each document when we have an array of more than 10k documents**?

> Note - I understand that there is one way ; use Bulk API but I could not find a way to post the Array of JSON Documents via that where each document in array will be treated as a different document.

Thanks in advance .

---

<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 5, 2017, 10:00pm UTC](https://discuss.elastic.co/t/how-to-post-json-array-to-elastic-search/54851/8 "2017-07-05T22:00:15Z")

</div>


