# Unable to upload json file to elastic search

**URL:** <https://discuss.elastic.co/t/unable-to-upload-json-file-to-elastic-search/280712>\
**Category:** Elasticsearch\
**Created:** [August 7, 2021, 6:17pm UTC](https://discuss.elastic.co/t/unable-to-upload-json-file-to-elastic-search/280712 "2021-08-07T18:17:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmunka](https://avatars.discourse-cdn.com/v4/letter/c/c89c15/32.png) [@cmunka](https://discuss.elastic.co/u/cmunka)\
**Post date:** [August 7, 2021, 6:17pm UTC](https://discuss.elastic.co/t/unable-to-upload-json-file-to-elastic-search/280712/1 "2021-08-07T18:17:50Z")

</div>

I am trying to upload a jsonl file to elastic search via \_bulk api using Python. Below is my code.!

Error:

 ![Screenshot 2021-08-07 at 11.43.12 PM](https://us1.discourse-cdn.com/elastic/original/3X/b/8/b8933f46973a2ab957dc684258fda122b3d69c67.png)

Data is having multiple json object:

 ![Screenshot 2021-08-07 at 11.45.51 PM](https://us1.discourse-cdn.com/elastic/original/3X/9/f/9f4dc801d7000e12e4cb9b777be6572fa9e5bfdb.png)

##code##

```auto
# -*- coding: utf-8 -*-
import requests
import json

url = "http://localhost:9200/tweets/_doc/_bulk"

data=[]
with open('bulk.json', 'rb') as json_file:
    for jsonObj in json_file:
        studentDict = json.loads(jsonObj)
        data.append(studentDict)
headers = {
  'Content-Type': 'application/x-ndjson', 'data':'binary'
}

response = requests.post( url, headers=headers, json=data)

print(response.text)

```

Please suggest me the solution for this. Thanks!

---

<div class="post-metadata">

**Author:** ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)\
**Post date:** [August 8, 2021, 3:27pm UTC](https://discuss.elastic.co/t/unable-to-upload-json-file-to-elastic-search/280712/2 "2021-08-08T15:27:20Z")

</div>

You are not trying to upload `json`, your **data** variable is a list, and you are trying to send this list to elasticsearch, which is not supported.

This is what you are trying to send:

```auto
[{json_object}, {json_object}, {json_object}, {json_object}, {json_object}]

```

You need to send in this format:

```auto
{json_object}
{json_object}
{json_object}
{json_object}

```

You will need to change your code.

The bellow line will convert a list wiht json elements into a string delimited by new lines:

```auto
bulk_data = '\n'.join(json.dumps(d) for d in data) + "\n"

```

I did not test it, but you could try to send this request:

```auto
response = requests.post( url, headers=headers, data=bulk_data)

```

But your problem is with your code, it is not related to elasticsearch, there are better places to get coding help.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [August 8, 2021, 11:19pm UTC](https://discuss.elastic.co/t/unable-to-upload-json-file-to-elastic-search/280712/3 "2021-08-08T23:19:27Z")

</div>

Welcome to our community! 😃

Please don't post pictures of text or code. They are difficult to read, impossible to search and replicate (if it's code), and some people may not be even able to see them 🙂

---

<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:** [September 5, 2021, 11:19pm UTC](https://discuss.elastic.co/t/unable-to-upload-json-file-to-elastic-search/280712/4 "2021-09-05T23:19:42Z")

</div>

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