Python-将json文件插入elasticsearch

时间:2018-10-17 15:51:07

标签: python json elasticsearch

我在文件中有这个Json:

{
   "name": "Huntington Beach",
   "beach": "50 N of Santa Ana River",
   "SampleDate": "02/01/18",
   "ParameterCode": "Enterococcus",
   "Qualifier": "=",
   "Result": "8",
   "Units": "CFU/100ml"
 },

我编写了此脚本,该脚本应打印所有json文本:

from pprint import pprint
import requests
import urllib
import json
from elasticsearch import Elasticsearch
from elasticsearch import helpers
from elasticsearch.serializer import JSONSerializer
import os,sys

directory = '/home/Documents/folder/myfile.json'

es = Elasticsearch([{'host': '', 'port': }])

with open(directory, 'r', encoding='utf-8') as f:
    data=json.loads(f.read())
pprint(data)

es.index(index='my_index', doc_type='doc', body=data)

它不起作用,您能帮我吗?这就是发生的情况:

  

提高JSONDecodeError(“额外数据”,s,结束)

     

json.decoder.JSONDecodeError:额外数据:

1 个答案:

答案 0 :(得分:0)

如@dwjv所述,您的json文件无效:

{
   "name": "Huntington Beach",
   "beach": "50 N of Santa Ana River",
   "SampleDate": "02/01/18",
   "ParameterCode": "Enterococcus",
   "Qualifier": "=",
   "Result": "8",
   "Units": "CFU/100ml"
 }, <---trailing comma

只需将其删除,您就应该可以了。