# Is there any response.to\_json() method in es python client?

**URL:** <https://discuss.elastic.co/t/is-there-any-response-to-json-method-in-es-python-client/47493>\
**Category:** Elasticsearch\
**Created:** [April 15, 2016, 11:07am UTC](https://discuss.elastic.co/t/is-there-any-response-to-json-method-in-es-python-client/47493 "2016-04-15T11:07:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Morriaty](https://avatars.discourse-cdn.com/v4/letter/m/8e8cbc/32.png) [@Morriaty](https://discuss.elastic.co/u/Morriaty)\
**Post date:** [April 15, 2016, 11:07am UTC](https://discuss.elastic.co/t/is-there-any-response-to-json-method-in-es-python-client/47493/1 "2016-04-15T11:07:23Z")

</div>

I'm using es python client `elasticsearch-dsl-py`.

```python
def post()
    query = Search().query(some_query)
    response = query.execute()
    return response

```

The code raised errors because `response` was a generator, while I hope it returned a json object.  
So I wanna know if there was a method to serialize `response` into json.

---

<div class="post-metadata">

**Author:** ![honzakral](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/honzakral/32/44958_2.png) [@honzakral](https://discuss.elastic.co/u/honzakral)\
**Post date:** [April 18, 2016, 2:56pm UTC](https://discuss.elastic.co/t/is-there-any-response-to-json-method-in-es-python-client/47493/2 "2016-04-18T14:56:56Z")

</div>

Not into json per say, but you can always get the underlying `dict` by calling `response.to_dict()` just as you can for any DSL object like `Search` or `Query`.

out of curiosity, can I ask why you need the json? Thanks!

---

<div class="post-metadata">

**Author:** ![Morriaty](https://avatars.discourse-cdn.com/v4/letter/m/8e8cbc/32.png) [@Morriaty](https://discuss.elastic.co/u/Morriaty)\
**Post date:** [April 19, 2016, 1:32am UTC](https://discuss.elastic.co/t/is-there-any-response-to-json-method-in-es-python-client/47493/3 "2016-04-19T01:32:23Z")

</div>

Hi @honzakral  
I am writing [APIs.So](http://APIs.So) I need json response for the front-end.

I know `to_dict()` method. What I am using now is `json.dumps(response.to_dict())`. I supposed it's a bit verbose, which packed es json response into py dict and by next turn it back into json. Why can't I get the json response directly?

---

<div class="post-metadata">

**Author:** ![honzakral](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/honzakral/32/44958_2.png) [@honzakral](https://discuss.elastic.co/u/honzakral)\
**Post date:** [April 19, 2016, 6:13am UTC](https://discuss.elastic.co/t/is-there-any-response-to-json-method-in-es-python-client/47493/4 "2016-04-19T06:13:51Z")

</div>

We don't keep the json around in python. If you just want the json back what you'd need to do is use the low-level API (`elasticsearch-py`) and supply your own serializer that doesn't actually deserialize the json and just passes it through. To still get the benefits of the dsl library you can always serialize the `Search` object (by calling `to_dict` on it) and passing it into [search](http://elasticsearch-py.readthedocs.org/en/master/api.html#elasticsearch.Elasticsearch.search) method manually like `.execute` does.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:58pm UTC](https://discuss.elastic.co/t/is-there-any-response-to-json-method-in-es-python-client/47493/5 "2017-07-05T22:58:14Z")

</div>


