Unable to upload json file to elastic search

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

Error:

Data is having multiple json object:

##code##

# -*- 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!

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:

[{json_object}, {json_object}, {json_object}, {json_object}, {json_object}]

You need to send in this format:

{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:

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:

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.

Welcome to our community! :smiley:

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 :slight_smile:

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