简体   繁体   中英

Python - insert json file into elasticsearch

I have this Json in a file:

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

I wrote this script which should print all of the json text:

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)

It doesn't work, can you help me? This is what happens:

raise JSONDecodeError("Extra data", s, end)

json.decoder.JSONDecodeError: Extra data:

As stated by @dwjv, your json file is not valid:

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

Just remove it and you should be good to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM