0

I wrote a simple python script to put the JSON file to Elasticsearch.I want to store it based on the id field I am extracting from the JSON file. But when I try to insert into elastic search.It raises an error TypeError: expected string or buffer

Here is the code I am working on...

#! /usr/bin/python

import requests
from elasticsearch import Elasticsearch
import json
es = Elasticsearch([{'host':'localhost','port':9200}])
r = requests.get('http://127.0.0.1:9200')
i = 1
if r.status_code == 200:

        with open('d1.json') as json_data:
                d = json.load(json_data)

                for i in d['tc'][0]['i]['f']['h']:
                        if i['name'] == 'm':
                                m = i['value']
                                dope=str(m)
                                print dope
                                print type(dope)
                                #print type(md5)
                                es.index(index='lab', doc_type='report',id=dope,body=json.loads(json_data))

Error Log:

44d88612fea8a8f36de82e1278abb02f
<type 'str'>
Traceback (most recent call last):
  File "elastic_insert.py", line 22, in <module>
    es.index(index='labs', doc_type='report',id=dope,body=json.loads(json_data))
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer

Any suggestions on how to solve this error.I even tried to convert the m to int but it gave another error.

int(m)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '44d88612fea8a8f36de82e1278abb02f'

P.S: ElasticSearch service is up and running.

1
  • Not being a python expert, the problem comes obviously from the json.loads method. Care to share the json file you use? Commented Mar 10, 2017 at 11:39

1 Answer 1

2

The problem is not related to the id. the problem is with "json_data". it is a file stream so you need json.load and not json.loads in your es.index

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.