When trying to write bucket contents to a JSON output file, I encounter the error
Traceback (most recent call last):
...
...
File "/home/user/.pyenv/versions/3.8.1/lib/python3.8/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/home/user/.pyenv/versions/3.8.1/lib/python3.8/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/home/user/.pyenv/versions/3.8.1/lib/python3.8/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/home/user/.pyenv/versions/3.8.1/lib/python3.8/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type FieldBucket is not JSON serializable
Here's my code:
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search
from datetime import date, timedelta
import json
client = Elasticsearch([{
'host': 'localhost',
'port': 9200,
'use_ssl': True,
'verify_certs': False,
'timeout': 600
}])
interval = "1h"
num_of_days = 7
start_date = (date.today() - timedelta(days=num_of_days)).strftime("%Y-%m-%d")
end_date = date.today().strftime("%Y-%m-%d")
s = Search(using=client, index="nginx_access_log*") \
.filter("range", **{'upstream_status': {'gte': 200, 'lte': 399}}) \
.filter("range", **{'@timestamp': {'gte': start_date, 'lt': end_date}}) \
.params(ignore_throttled="false")
s.aggs.bucket('time', 'date_histogram', field='@timestamp', interval=interval) \
.metric('1', 'cardinality', field='host_header')
response = s.execute()
print(f"Found {len(response.aggregations.time.buckets)} buckets matching the criteria")
for entry in response.aggregations.time.buckets:
content.append(entry)
fp = open(self.path, "w")
json.dump(content, fp)
fp.close()
The error occurs at the json.dump(content, fp) part.
I've checked the documentation as best I can, and I can't see any method like to_json() which I could use. Any ideas?
There is a to_dict method on all the objects, including the top level response which will give you a python dict object which should be json serializable.
Hope this helps!
Ah, I must have missed that! Thanks! 馃槉
Most helpful comment
There is a
to_dictmethod on all the objects, including the top levelresponsewhich will give you a pythondictobject which should bejsonserializable.Hope this helps!