# From JSON to Java API and back

**URL:** <https://discuss.elastic.co/t/from-json-to-java-api-and-back/4064>\
**Category:** Elasticsearch\
**Created:** [March 9, 2011, 1:54pm UTC](https://discuss.elastic.co/t/from-json-to-java-api-and-back/4064 "2011-03-09T13:54:06Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![paranoiabla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paranoiabla/32/2160_2.png) [@paranoiabla](https://discuss.elastic.co/u/paranoiabla)\
**Post date:** [March 9, 2011, 1:54pm UTC](https://discuss.elastic.co/t/from-json-to-java-api-and-back/4064/1 "2011-03-09T13:54:06Z")

</div>

Hi there,

Im using the Java API and I want to know what is the best way to  
convert the SearchResponse object into JSON format. Also If Im using  
the JSON API and I receive back a JSON response, is there a way to  
convert it to a SearchResponse.

Thank you for your time.

Cheers, Petar.

---

<div class="post-metadata">

**Author:** ![Alois\_Cochard](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alois_cochard/32/3176_2.png) [@Alois\_Cochard](https://discuss.elastic.co/u/Alois_Cochard)\
**Post date:** [March 9, 2011, 4:47pm UTC](https://discuss.elastic.co/t/from-json-to-java-api-and-back/4064/2 "2011-03-09T16:47:47Z")

</div>

Hi Petar,

SearchResponse has a writeTo and a readFrom methods taking a  
Stream[Input/Output], if think it should do the job ?

Concerning the deserialization I think it's better to use the static  
method 'readSearchResponse' that return a new SearchResponse instead  
of using the readFrom instance method.

Hope that's help !

Cheers,

Alois Cochard

> **[Alois Cochard's Blog](http://aloiscochard.blogspot.com)**
>
> Computer Development

[http://www.twitter.com/aloiscochard](http://www.twitter.com/aloiscochard)

On Mar 9, 2:54 pm, paranoiabla [paranoia...@gmail.com](mailto:paranoia...@gmail.com) wrote:

> Hi there,
> 
> Im using the Java API and I want to know what is the best way to  
> convert the SearchResponse object into JSON format. Also If Im using  
> the JSON API and I receive back a JSON response, is there a way to  
> convert it to a SearchResponse.
> 
> Thank you for your time.
> 
> Cheers, Petar.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [March 9, 2011, 6:32pm UTC](https://discuss.elastic.co/t/from-json-to-java-api-and-back/4064/3 "2011-03-09T18:32:29Z")

</div>

SearchResponse implements the ToXContent interface, which means you can use XContentFactory to create a json builder, pass it to the toXContent method, and then get its binary / string representation. There is no way to parse a json to a SearchResponse.  
On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:

> Hi there,
> 
> Im using the Java API and I want to know what is the best way to  
> convert the SearchResponse object into JSON format. Also If Im using  
> the JSON API and I receive back a JSON response, is there a way to  
> convert it to a SearchResponse.
> 
> Thank you for your time.
> 
> Cheers, Petar.

---

<div class="post-metadata">

**Author:** ![Ridvan\_Gyundogan](https://avatars.discourse-cdn.com/v4/letter/r/e495f1/32.png) [@Ridvan\_Gyundogan](https://discuss.elastic.co/u/Ridvan_Gyundogan)\
**Post date:** [March 9, 2011, 7:37pm UTC](https://discuss.elastic.co/t/from-json-to-java-api-and-back/4064/4 "2011-03-09T19:37:15Z")

</div>

@Shay  
There should be a way to parse json request to something like SearchRequest  
object, I think this is the second part of the question.  
When we send a curl search request you probably convert the json to java  
SearchRequest object?

On Wed, Mar 9, 2011 at 8:32 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)wrote:

> SearchResponse implements the ToXContent interface, which means you can  
> use XContentFactory to create a json builder, pass it to the toXContent  
> method, and then get its binary / string representation. There is no way to  
> parse a json to a SearchResponse.
> 
> On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:
> 
> Hi there,
> 
> Im using the Java API and I want to know what is the best way to  
> convert the SearchResponse object into JSON format. Also If Im using  
> the JSON API and I receive back a JSON response, is there a way to  
> convert it to a SearchResponse.
> 
> Thank you for your time.
> 
> Cheers, Petar.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [March 9, 2011, 7:45pm UTC](https://discuss.elastic.co/t/from-json-to-java-api-and-back/4064/5 "2011-03-09T19:45:09Z")

</div>

The question was about SearchResponse, not SearchRequest :). Parsing the search request from json is simple, just pass it using the SearchRequset#source(...) method.

Note, you will still need to explicitly set the search type as its not part of the json "body" request.  
On Wednesday, March 9, 2011 at 9:37 PM, Ridvan Gyundogan wrote:

> @Shay  
> There should be a way to parse json request to something like SearchRequest object, I think this is the second part of the question.  
> When we send a curl search request you probably convert the json to java SearchRequest object?
> 
> On Wed, Mar 9, 2011 at 8:32 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:
> 
> > SearchResponse implements the ToXContent interface, which means you can use XContentFactory to create a json builder, pass it to the toXContent method, and then get its binary / string representation. There is no way to parse a json to a SearchResponse.  
> > On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:
> > 
> > > Hi there,
> > > 
> > > Im using the Java API and I want to know what is the best way to  
> > > convert the SearchResponse object into JSON format. Also If Im using  
> > > the JSON API and I receive back a JSON response, is there a way to  
> > > convert it to a SearchResponse.
> > > 
> > > Thank you for your time.
> > > 
> > > Cheers, Petar.

---

<div class="post-metadata">

**Author:** ![paranoiabla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paranoiabla/32/2160_2.png) [@paranoiabla](https://discuss.elastic.co/u/paranoiabla)\
**Post date:** [March 10, 2011, 8:31am UTC](https://discuss.elastic.co/t/from-json-to-java-api-and-back/4064/6 "2011-03-10T08:31:25Z")

</div>

Hi guys,

none of the suggestions seemed to work for me (or most likely Im doing  
something wrong).  
I first tried:

```
    BytesStreamOutput stream = new BytesStreamOutput();

    buildRequest(request).execute().actionGet().writeTo(stream);

    stream.close();

    String result = new String(stream.copiedByteArray());

    System.out.println("!!" + result);

```

but the result I get is a semi-json containing some just random weird  
bytes (you can see the result.txt attached to this email)

Then I tried using:

```
    XContentBuilder builder =

```

XContentFactory.contentBuilder(XContentType.JSON);

```
    buildRequest(request).execute().actionGet().toXContent(builder,

```

ToXContent.EMPTY\_PARAMS);

```
    String result = builder.string();

    System.out.println("!!" + result);

```

but I get this exception

org.elasticsearch.common.jackson.JsonGenerationException: Can not  
write a field name, expecting a value  
org.elasticsearch.common.jackson.impl.JsonGeneratorBase.\_reportError(JsonGeneratorBase.java:481)  
org.elasticsearch.common.jackson.impl.Utf8Generator.writeFieldName(Utf8Generator.java:196)  
org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:71)  
org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:221)  
org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:357)  
org.elasticsearch.action.search.SearchResponse.toXContent(SearchResponse.java:229)

at this line  
buildRequest(request).execute().actionGet().toXContent(builder,  
ToXContent.EMPTY\_PARAMS);

Does anyone know what Im doing wrong?

2011/3/9 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)

> The question was about SearchResponse, not SearchRequest :). Parsing the search request from json is simple, just pass it using the SearchRequset#source(...) method.  
> Note, you will still need to explicitly set the search type as its not part of the json "body" request.
> 
> On Wednesday, March 9, 2011 at 9:37 PM, Ridvan Gyundogan wrote:
> 
> @Shay  
> There should be a way to parse json request to something like SearchRequest object, I think this is the second part of the question.  
> When we send a curl search request you probably convert the json to java SearchRequest object?
> 
> On Wed, Mar 9, 2011 at 8:32 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:
> 
> SearchResponse implements the ToXContent interface, which means you can use XContentFactory to create a json builder, pass it to the toXContent method, and then get its binary / string representation. There is no way to parse a json to a SearchResponse.
> 
> On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:
> 
> Hi there,
> 
> Im using the Java API and I want to know what is the best way to  
> convert the SearchResponse object into JSON format. Also If Im using  
> the JSON API and I receive back a JSON response, is there a way to  
> convert it to a SearchResponse.
> 
> Thank you for your time.
> 
> Cheers, Petar.

--  
Regards, Petar!  
Karlovo, Bulgaria.

* * *

| Author @ Manning Publications.  
| CEO @ Phamola  
| BGJUG-Bulgarian Java User Group Leader.  
| Apache Maven Developer.  
| Apache Jakarta PMC member.  
| Jakarta Cactus Lead Developer.  
| Codehaus Plexus Developer  
| Blogger: [Oracle.com Outage](http://weblogs.java.net/blog/paranoiabla/)

* * *

Public PGP Key at:  
[https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [March 10, 2011, 8:44am UTC](https://discuss.elastic.co/t/from-json-to-java-api-and-back/4064/7 "2011-03-10T08:44:48Z")

</div>

You need to wrap the toXContent call with startObject and endObject. Something like this:

builder.startObject();  
response.toXContent(builder, request);  
builder.endObject();

The stream is an internal serialization format, not a json one.  
On Thursday, March 10, 2011 at 10:31 AM, Petar Tahchiev wrote:  
Hi guys,

> none of the suggestions seemed to work for me (or most likely Im doing  
> something wrong).  
> I first tried:
> 
> BytesStreamOutput stream = new BytesStreamOutput();
> 
> buildRequest(request).execute().actionGet().writeTo(stream);
> 
> stream.close();
> 
> String result = new String(stream.copiedByteArray());
> 
> System.out.println("!!" + result);
> 
> but the result I get is a semi-json containing some just random weird  
> bytes (you can see the result.txt attached to this email)
> 
> Then I tried using:
> 
> XContentBuilder builder =  
> XContentFactory.contentBuilder(XContentType.JSON);
> 
> buildRequest(request).execute().actionGet().toXContent(builder,  
> ToXContent.EMPTY\_PARAMS);
> 
> String result = builder.string();
> 
> System.out.println("!!" + result);
> 
> but I get this exception
> 
> org.elasticsearch.common.jackson.JsonGenerationException: Can not  
> write a field name, expecting a value  
> org.elasticsearch.common.jackson.impl.JsonGeneratorBase.\_reportError(JsonGeneratorBase.java:481)  
> org.elasticsearch.common.jackson.impl.Utf8Generator.writeFieldName(Utf8Generator.java:196)  
> org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:71)  
> org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:221)  
> org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:357)  
> org.elasticsearch.action.search.SearchResponse.toXContent(SearchResponse.java:229)
> 
> at this line  
> buildRequest(request).execute().actionGet().toXContent(builder,  
> ToXContent.EMPTY\_PARAMS);
> 
> Does anyone know what Im doing wrong?
> 
> 2011/3/9 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)
> 
> > The question was about SearchResponse, not SearchRequest :). Parsing the search request from json is simple, just pass it using the SearchRequset#source(...) method.  
> > Note, you will still need to explicitly set the search type as its not part of the json "body" request.
> > 
> > On Wednesday, March 9, 2011 at 9:37 PM, Ridvan Gyundogan wrote:
> > 
> > @Shay  
> > There should be a way to parse json request to something like SearchRequest object, I think this is the second part of the question.  
> > When we send a curl search request you probably convert the json to java SearchRequest object?
> > 
> > On Wed, Mar 9, 2011 at 8:32 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:
> > 
> > SearchResponse implements the ToXContent interface, which means you can use XContentFactory to create a json builder, pass it to the toXContent method, and then get its binary / string representation. There is no way to parse a json to a SearchResponse.
> > 
> > On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:
> > 
> > Hi there,
> > 
> > Im using the Java API and I want to know what is the best way to  
> > convert the SearchResponse object into JSON format. Also If Im using  
> > the JSON API and I receive back a JSON response, is there a way to  
> > convert it to a SearchResponse.
> > 
> > Thank you for your time.
> > 
> > Cheers, Petar.
> 
> --  
> Regards, Petar!  
> Karlovo, Bulgaria.
> 
> * * *
> 
> | Author @ Manning Publications.  
> | CEO @ Phamola  
> | BGJUG-Bulgarian Java User Group Leader.  
> | Apache Maven Developer.  
> | Apache Jakarta PMC member.  
> | Jakarta Cactus Lead Developer.  
> | Codehaus Plexus Developer  
> | Blogger: [Oracle.com Outage](http://weblogs.java.net/blog/paranoiabla/)
> 
> * * *
> 
> Public PGP Key at:  
> [https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
> Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611
> 
> Attachments:
> 
> - result.txt

---

<div class="post-metadata">

**Author:** ![paranoiabla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paranoiabla/32/2160_2.png) [@paranoiabla](https://discuss.elastic.co/u/paranoiabla)\
**Post date:** [March 10, 2011, 8:53am UTC](https://discuss.elastic.co/t/from-json-to-java-api-and-back/4064/8 "2011-03-10T08:53:28Z")

</div>

OK, this time no exception is thrown, but instead I get again weird  
characters, like:  
"🙂 򄱵ery򇦩ltered򀺈match\_all򻻅filter򂺻󻻆explain#facets򃮡me򄴥rms򄦩eldCnamesizeԻ󻻻"took":4,"

which is the beginning of the String.

Do you have any idea what could cause that?

2011/3/10 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com):

> You need to wrap the toXContent call with startObject and endObject.  
> Something like this:  
> builder.startObject();  
> response.toXContent(builder, request);  
> builder.endObject();  
> The stream is an internal serialization format, not a json one.
> 
> On Thursday, March 10, 2011 at 10:31 AM, Petar Tahchiev wrote:
> 
> Hi guys,
> 
> none of the suggestions seemed to work for me (or most likely Im doing  
> something wrong).  
> I first tried:
> 
> ```
> BytesStreamOutput stream = new BytesStreamOutput();
> 
> buildRequest(request).execute().actionGet().writeTo(stream);
> 
> stream.close();
> 
> String result = new String(stream.copiedByteArray());
> 
> System.out.println("!!" + result);
> 
> ```
> 
> but the result I get is a semi-json containing some just random weird  
> bytes (you can see the result.txt attached to this email)
> 
> Then I tried using:
> 
> ```
> XContentBuilder builder =
> 
> ```
> 
> XContentFactory.contentBuilder(XContentType.JSON);
> 
> ```
> buildRequest(request).execute().actionGet().toXContent(builder,
> 
> ```
> 
> ToXContent.EMPTY\_PARAMS);
> 
> ```
> String result = builder.string();
> 
> System.out.println("!!" + result);
> 
> ```
> 
> but I get this exception
> 
> org.elasticsearch.common.jackson.JsonGenerationException: Can not  
> write a field name, expecting a value  
> org.elasticsearch.common.jackson.impl.JsonGeneratorBase.\_reportError(JsonGeneratorBase.java:481)  
> org.elasticsearch.common.jackson.impl.Utf8Generator.writeFieldName(Utf8Generator.java:196)  
> org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:71)  
> org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:221)  
> org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:357)  
> org.elasticsearch.action.search.SearchResponse.toXContent(SearchResponse.java:229)
> 
> at this line  
> buildRequest(request).execute().actionGet().toXContent(builder,  
> ToXContent.EMPTY\_PARAMS);
> 
> Does anyone know what Im doing wrong?
> 
> 2011/3/9 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)
> 
> The question was about SearchResponse, not SearchRequest :). Parsing the  
> search request from json is simple, just pass it using the  
> SearchRequset#source(...) method.  
> Note, you will still need to explicitly set the search type as its not part  
> of the json "body" request.
> 
> On Wednesday, March 9, 2011 at 9:37 PM, Ridvan Gyundogan wrote:
> 
> @Shay  
> There should be a way to parse json request to something like SearchRequest  
> object, I think this is the second part of the question.  
> When we send a curl search request you probably convert the json to java  
> SearchRequest object?
> 
> On Wed, Mar 9, 2011 at 8:32 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)  
> wrote:
> 
> SearchResponse implements the ToXContent interface, which means you can use  
> XContentFactory to create a json builder, pass it to the toXContent method,  
> and then get its binary / string representation. There is no way to parse a  
> json to a SearchResponse.
> 
> On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:
> 
> Hi there,
> 
> Im using the Java API and I want to know what is the best way to  
> convert the SearchResponse object into JSON format. Also If Im using  
> the JSON API and I receive back a JSON response, is there a way to  
> convert it to a SearchResponse.
> 
> Thank you for your time.
> 
> Cheers, Petar.
> 
> --  
> Regards, Petar!  
> Karlovo, Bulgaria.
> 
> * * *
> 
> | Author @ Manning Publications.  
> | CEO @ Phamola  
> | BGJUG-Bulgarian Java User Group Leader.  
> | Apache Maven Developer.  
> | Apache Jakarta PMC member.  
> | Jakarta Cactus Lead Developer.  
> | Codehaus Plexus Developer  
> | Blogger: [Oracle.com Outage](http://weblogs.java.net/blog/paranoiabla/)
> 
> * * *
> 
> Public PGP Key at:  
> [https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
> Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611
> 
> Attachments:
> 
> - result.txt

--  
Regards, Petar!  
Karlovo, Bulgaria.

* * *

| Author @ Manning Publications.  
| CEO @ Phamola  
| BGJUG-Bulgarian Java User Group Leader.  
| Apache Maven Developer.  
| Apache Jakarta PMC member.  
| Jakarta Cactus Lead Developer.  
| Codehaus Plexus Developer  
| Blogger: [Oracle.com Outage](http://weblogs.java.net/blog/paranoiabla/)

* * *

Public PGP Key at:  
[https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

---

<div class="post-metadata">

**Author:** ![paranoiabla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paranoiabla/32/2160_2.png) [@paranoiabla](https://discuss.elastic.co/u/paranoiabla)\
**Post date:** [March 10, 2011, 8:55am UTC](https://discuss.elastic.co/t/from-json-to-java-api-and-back/4064/9 "2011-03-10T08:55:18Z")

</div>

I guess I have to send some parameters other than EMPTY\_PARAMS, but I  
have no idea what exactly.

2011/3/10 Petar Tahchiev [paranoiabla@gmail.com](mailto:paranoiabla@gmail.com):

> OK, this time no exception is thrown, but instead I get again weird  
> characters, like:  
> "🙂 򄱵ery򇦩ltered򀺈match\_all򻻅filter򂺻󻻆explain#…facets򃮡me򄴥rms򄦩eldCnameƒsizeԻ󻻻"took":4,"
> 
> which is the beginning of the String.
> 
> Do you have any idea what could cause that?
> 
> 2011/3/10 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com):
> 
> > You need to wrap the toXContent call with startObject and endObject.  
> > Something like this:  
> > builder.startObject();  
> > response.toXContent(builder, request);  
> > builder.endObject();  
> > The stream is an internal serialization format, not a json one.
> > 
> > On Thursday, March 10, 2011 at 10:31 AM, Petar Tahchiev wrote:
> > 
> > Hi guys,
> > 
> > none of the suggestions seemed to work for me (or most likely Im doing  
> > something wrong).  
> > I first tried:
> > 
> > ```
> > BytesStreamOutput stream = new BytesStreamOutput();
> > 
> > buildRequest(request).execute().actionGet().writeTo(stream);
> > 
> > stream.close();
> > 
> > String result = new String(stream.copiedByteArray());
> > 
> > System.out.println("!!" + result);
> > 
> > ```
> > 
> > but the result I get is a semi-json containing some just random weird  
> > bytes (you can see the result.txt attached to this email)
> > 
> > Then I tried using:
> > 
> > ```
> > XContentBuilder builder =
> > 
> > ```
> > 
> > XContentFactory.contentBuilder(XContentType.JSON);
> > 
> > ```
> > buildRequest(request).execute().actionGet().toXContent(builder,
> > 
> > ```
> > 
> > ToXContent.EMPTY\_PARAMS);
> > 
> > ```
> > String result = builder.string();
> > 
> > System.out.println("!!" + result);
> > 
> > ```
> > 
> > but I get this exception
> > 
> > org.elasticsearch.common.jackson.JsonGenerationException: Can not  
> > write a field name, expecting a value  
> > org.elasticsearch.common.jackson.impl.JsonGeneratorBase.\_reportError(JsonGeneratorBase.java:481)  
> > org.elasticsearch.common.jackson.impl.Utf8Generator.writeFieldName(Utf8Generator.java:196)  
> > org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:71)  
> > org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:221)  
> > org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:357)  
> > org.elasticsearch.action.search.SearchResponse.toXContent(SearchResponse.java:229)
> > 
> > at this line  
> > buildRequest(request).execute().actionGet().toXContent(builder,  
> > ToXContent.EMPTY\_PARAMS);
> > 
> > Does anyone know what Im doing wrong?
> > 
> > 2011/3/9 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)
> > 
> > The question was about SearchResponse, not SearchRequest :). Parsing the  
> > search request from json is simple, just pass it using the  
> > SearchRequset#source(...) method.  
> > Note, you will still need to explicitly set the search type as its not part  
> > of the json "body" request.
> > 
> > On Wednesday, March 9, 2011 at 9:37 PM, Ridvan Gyundogan wrote:
> > 
> > @Shay  
> > There should be a way to parse json request to something like SearchRequest  
> > object, I think this is the second part of the question.  
> > When we send a curl search request you probably convert the json to java  
> > SearchRequest object?
> > 
> > On Wed, Mar 9, 2011 at 8:32 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)  
> > wrote:
> > 
> > SearchResponse implements the ToXContent interface, which means you can use  
> > XContentFactory to create a json builder, pass it to the toXContent method,  
> > and then get its binary / string representation. There is no way to parse a  
> > json to a SearchResponse.
> > 
> > On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:
> > 
> > Hi there,
> > 
> > Im using the Java API and I want to know what is the best way to  
> > convert the SearchResponse object into JSON format. Also If Im using  
> > the JSON API and I receive back a JSON response, is there a way to  
> > convert it to a SearchResponse.
> > 
> > Thank you for your time.
> > 
> > Cheers, Petar.
> > 
> > --  
> > Regards, Petar!  
> > Karlovo, Bulgaria.
> > 
> > * * *
> > 
> > | Author @ Manning Publications.  
> > | CEO @ Phamola  
> > | BGJUG-Bulgarian Java User Group Leader.  
> > | Apache Maven Developer.  
> > | Apache Jakarta PMC member.  
> > | Jakarta Cactus Lead Developer.  
> > | Codehaus Plexus Developer  
> > | Blogger: [http://weblogs.java.net/blog/paranoiabla/](http://weblogs.java.net/blog/paranoiabla/)
> > 
> > * * *
> > 
> > Public PGP Key at:  
> > [https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
> > Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611
> > 
> > Attachments:
> > 
> > - result.txt
> 
> --  
> Regards, Petar!  
> Karlovo, Bulgaria.
> 
> * * *
> 
> | Author @ Manning Publications.  
> | CEO @ Phamola  
> | BGJUG-Bulgarian Java User Group Leader.  
> | Apache Maven Developer.  
> | Apache Jakarta PMC member.  
> | Jakarta Cactus Lead Developer.  
> | Codehaus Plexus Developer  
> | Blogger: [http://weblogs.java.net/blog/paranoiabla/](http://weblogs.java.net/blog/paranoiabla/)
> 
> * * *
> 
> Public PGP Key at:  
> [https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
> Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

--  
Regards, Petar!  
Karlovo, Bulgaria.

* * *

| Author @ Manning Publications.  
| CEO @ Phamola  
| BGJUG-Bulgarian Java User Group Leader.  
| Apache Maven Developer.  
| Apache Jakarta PMC member.  
| Jakarta Cactus Lead Developer.  
| Codehaus Plexus Developer  
| Blogger: [http://weblogs.java.net/blog/paranoiabla/](http://weblogs.java.net/blog/paranoiabla/)

* * *

Public PGP Key at:  
[https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [March 10, 2011, 12:17pm UTC](https://discuss.elastic.co/t/from-json-to-java-api-and-back/4064/10 "2011-03-10T12:17:22Z")

</div>

You don't need to send any parameters. Can you _gist_ the code you use now?  
On Thursday, March 10, 2011 at 10:55 AM, Petar Tahchiev wrote:

> I guess I have to send some parameters other than EMPTY\_PARAMS, but I  
> have no idea what exactly.
> 
> 2011/3/10 Petar Tahchiev [paranoiabla@gmail.com](mailto:paranoiabla@gmail.com):
> 
> > OK, this time no exception is thrown, but instead I get again weird  
> > characters, like:  
> > "🙂 ò±µeryò¦©lteredòºmatch\_allò»»filteròº»ó»»explain#â¦facetsò®¡meò´¥rmsò¦©eldCnameÆsizeÔ»ó»»»"took":4,"
> > 
> > which is the beginning of the String.
> > 
> > Do you have any idea what could cause that?
> > 
> > 2011/3/10 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com):
> > 
> > > You need to wrap the toXContent call with startObject and endObject.  
> > > Something like this:  
> > > builder.startObject();  
> > > response.toXContent(builder, request);  
> > > builder.endObject();  
> > > The stream is an internal serialization format, not a json one.
> > > 
> > > On Thursday, March 10, 2011 at 10:31 AM, Petar Tahchiev wrote:
> > > 
> > > Hi guys,
> > > 
> > > none of the suggestions seemed to work for me (or most likely Im doing  
> > > something wrong).  
> > > I first tried:
> > > 
> > > BytesStreamOutput stream = new BytesStreamOutput();
> > > 
> > > buildRequest(request).execute().actionGet().writeTo(stream);
> > > 
> > > stream.close();
> > > 
> > > String result = new String(stream.copiedByteArray());
> > > 
> > > System.out.println("!!" + result);
> > > 
> > > but the result I get is a semi-json containing some just random weird  
> > > bytes (you can see the result.txt attached to this email)
> > > 
> > > Then I tried using:
> > > 
> > > XContentBuilder builder =  
> > > XContentFactory.contentBuilder(XContentType.JSON);
> > > 
> > > buildRequest(request).execute().actionGet().toXContent(builder,  
> > > ToXContent.EMPTY\_PARAMS);
> > > 
> > > String result = builder.string();
> > > 
> > > System.out.println("!!" + result);
> > > 
> > > but I get this exception
> > > 
> > > org.elasticsearch.common.jackson.JsonGenerationException: Can not  
> > > write a field name, expecting a value  
> > > org.elasticsearch.common.jackson.impl.JsonGeneratorBase.\_reportError(JsonGeneratorBase.java:481)  
> > > org.elasticsearch.common.jackson.impl.Utf8Generator.writeFieldName(Utf8Generator.java:196)  
> > > org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:71)  
> > > org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:221)  
> > > org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:357)  
> > > org.elasticsearch.action.search.SearchResponse.toXContent(SearchResponse.java:229)
> > > 
> > > at this line  
> > > buildRequest(request).execute().actionGet().toXContent(builder,  
> > > ToXContent.EMPTY\_PARAMS);
> > > 
> > > Does anyone know what Im doing wrong?
> > > 
> > > 2011/3/9 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)
> > > 
> > > The question was about SearchResponse, not SearchRequest :). Parsing the  
> > > search request from json is simple, just pass it using the  
> > > SearchRequset#source(...) method.  
> > > Note, you will still need to explicitly set the search type as its not part  
> > > of the json "body" request.
> > > 
> > > On Wednesday, March 9, 2011 at 9:37 PM, Ridvan Gyundogan wrote:
> > > 
> > > @Shay  
> > > There should be a way to parse json request to something like SearchRequest  
> > > object, I think this is the second part of the question.  
> > > When we send a curl search request you probably convert the json to java  
> > > SearchRequest object?
> > > 
> > > On Wed, Mar 9, 2011 at 8:32 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)  
> > > wrote:
> > > 
> > > SearchResponse implements the ToXContent interface, which means you can use  
> > > XContentFactory to create a json builder, pass it to the toXContent method,  
> > > and then get its binary / string representation. There is no way to parse a  
> > > json to a SearchResponse.
> > > 
> > > On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:
> > > 
> > > Hi there,
> > > 
> > > Im using the Java API and I want to know what is the best way to  
> > > convert the SearchResponse object into JSON format. Also If Im using  
> > > the JSON API and I receive back a JSON response, is there a way to  
> > > convert it to a SearchResponse.
> > > 
> > > Thank you for your time.
> > > 
> > > Cheers, Petar.
> > > 
> > > --  
> > > Regards, Petar!  
> > > Karlovo, Bulgaria.
> > > 
> > > * * *
> > > 
> > > | Author @ Manning Publications.  
> > > | CEO @ Phamola  
> > > | BGJUG-Bulgarian Java User Group Leader.  
> > > | Apache Maven Developer.  
> > > | Apache Jakarta PMC member.  
> > > | Jakarta Cactus Lead Developer.  
> > > | Codehaus Plexus Developer  
> > > | Blogger: [http://weblogs.java.net/blog/paranoiabla/](http://weblogs.java.net/blog/paranoiabla/)
> > > 
> > > * * *
> > > 
> > > Public PGP Key at:  
> > > [https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
> > > Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611
> > > 
> > > Attachments:
> > > 
> > > - result.txt
> > 
> > --  
> > Regards, Petar!  
> > Karlovo, Bulgaria.
> > 
> > * * *
> > 
> > | Author @ Manning Publications.  
> > | CEO @ Phamola  
> > | BGJUG-Bulgarian Java User Group Leader.  
> > | Apache Maven Developer.  
> > | Apache Jakarta PMC member.  
> > | Jakarta Cactus Lead Developer.  
> > | Codehaus Plexus Developer  
> > | Blogger: [http://weblogs.java.net/blog/paranoiabla/](http://weblogs.java.net/blog/paranoiabla/)
> > 
> > * * *
> > 
> > Public PGP Key at:  
> > [https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
> > Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611
> 
> --  
> Regards, Petar!  
> Karlovo, Bulgaria.
> 
> * * *
> 
> | Author @ Manning Publications.  
> | CEO @ Phamola  
> | BGJUG-Bulgarian Java User Group Leader.  
> | Apache Maven Developer.  
> | Apache Jakarta PMC member.  
> | Jakarta Cactus Lead Developer.  
> | Codehaus Plexus Developer  
> | Blogger: [http://weblogs.java.net/blog/paranoiabla/](http://weblogs.java.net/blog/paranoiabla/)
> 
> * * *
> 
> Public PGP Key at:  
> [https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
> Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

---

<div class="post-metadata">

**Author:** ![paranoiabla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paranoiabla/32/2160_2.png) [@paranoiabla](https://discuss.elastic.co/u/paranoiabla)\
**Post date:** [March 10, 2011, 1:48pm UTC](https://discuss.elastic.co/t/from-json-to-java-api-and-back/4064/11 "2011-03-10T13:48:15Z")

</div>

check [gist:864108 · GitHub](https://gist.github.com/864108) for my code snippet

The expected JSON which should be createdshould look like this  
{

```
* took: 9
* timed_out: false
* _shards: {
      o total: 5
      o successful: 5
      o failed: 0
  }

```

..............  
instead after this code is invoked a content which looks like this is returned:  
🙂  
򄱵ery򇦩ltered򀺈match\_all򻻅filter򂺻󻻆explain#󻢴ook":1,"timed\_out":false,"\_shards":{"total":5,"successful":5,"failed":0},"hits":{"tot

so you see in the begining there are some strange non normally encoded  
symbols ... in the toXContent method of XContentBuilder I see that the  
method starts with adding a fields starting with TOOK... and etc  
if (scrollId != null) {  
builder.field(Fields.\_SCROLL\_ID, scrollId);  
}  
builder.field(Fields.TOOK, tookInMillis);  
builder.field(Fields.TIMED\_OUT, timedOut());  
(because the scroll\_id is null in my case ) so .. I dont know why at  
the end before took somewhere this strangly encoded query is passed. (  
it looks like my search query but the encoding is wrong ) )

Thanks with the help.

2011/3/10 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com):

> You don't need to send any parameters. Can you _gist_ the code you use now?
> 
> On Thursday, March 10, 2011 at 10:55 AM, Petar Tahchiev wrote:
> 
> I guess I have to send some parameters other than EMPTY\_PARAMS, but I  
> have no idea what exactly.
> 
> 2011/3/10 Petar Tahchiev [paranoiabla@gmail.com](mailto:paranoiabla@gmail.com):
> 
> OK, this time no exception is thrown, but instead I get again weird  
> characters, like:  
> "🙂  
> 򄱵ery򇦩ltered򀺈match\_all򻻅filter򂺻󻻆explain#…facets򃮡me򄴥rms򄦩eldCnameƒsizeԻ󻻻"took":4,"
> 
> which is the beginning of the String.
> 
> Do you have any idea what could cause that?
> 
> 2011/3/10 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com):
> 
> You need to wrap the toXContent call with startObject and endObject.  
> Something like this:  
> builder.startObject();  
> response.toXContent(builder, request);  
> builder.endObject();  
> The stream is an internal serialization format, not a json one.
> 
> On Thursday, March 10, 2011 at 10:31 AM, Petar Tahchiev wrote:
> 
> Hi guys,
> 
> none of the suggestions seemed to work for me (or most likely Im doing  
> something wrong).  
> I first tried:
> 
> ```
> BytesStreamOutput stream = new BytesStreamOutput();
> 
> buildRequest(request).execute().actionGet().writeTo(stream);
> 
> stream.close();
> 
> String result = new String(stream.copiedByteArray());
> 
> System.out.println("!!" + result);
> 
> ```
> 
> but the result I get is a semi-json containing some just random weird  
> bytes (you can see the result.txt attached to this email)
> 
> Then I tried using:
> 
> ```
> XContentBuilder builder =
> 
> ```
> 
> XContentFactory.contentBuilder(XContentType.JSON);
> 
> ```
> buildRequest(request).execute().actionGet().toXContent(builder,
> 
> ```
> 
> ToXContent.EMPTY\_PARAMS);
> 
> ```
> String result = builder.string();
> 
> System.out.println("!!" + result);
> 
> ```
> 
> but I get this exception
> 
> org.elasticsearch.common.jackson.JsonGenerationException: Can not  
> write a field name, expecting a value  
> org.elasticsearch.common.jackson.impl.JsonGeneratorBase.\_reportError(JsonGeneratorBase.java:481)  
> org.elasticsearch.common.jackson.impl.Utf8Generator.writeFieldName(Utf8Generator.java:196)  
> org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:71)  
> org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:221)  
> org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:357)  
> org.elasticsearch.action.search.SearchResponse.toXContent(SearchResponse.java:229)
> 
> at this line  
> buildRequest(request).execute().actionGet().toXContent(builder,  
> ToXContent.EMPTY\_PARAMS);
> 
> Does anyone know what Im doing wrong?
> 
> 2011/3/9 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)
> 
> The question was about SearchResponse, not SearchRequest :). Parsing the  
> search request from json is simple, just pass it using the  
> SearchRequset#source(...) method.  
> Note, you will still need to explicitly set the search type as its not part  
> of the json "body" request.
> 
> On Wednesday, March 9, 2011 at 9:37 PM, Ridvan Gyundogan wrote:
> 
> @Shay  
> There should be a way to parse json request to something like SearchRequest  
> object, I think this is the second part of the question.  
> When we send a curl search request you probably convert the json to java  
> SearchRequest object?
> 
> On Wed, Mar 9, 2011 at 8:32 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)  
> wrote:
> 
> SearchResponse implements the ToXContent interface, which means you can use  
> XContentFactory to create a json builder, pass it to the toXContent method,  
> and then get its binary / string representation. There is no way to parse a  
> json to a SearchResponse.
> 
> On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:
> 
> Hi there,
> 
> Im using the Java API and I want to know what is the best way to  
> convert the SearchResponse object into JSON format. Also If Im using  
> the JSON API and I receive back a JSON response, is there a way to  
> convert it to a SearchResponse.
> 
> Thank you for your time.
> 
> Cheers, Petar.
> 
> --  
> Regards, Petar!  
> Karlovo, Bulgaria.
> 
> * * *
> 
> | Author @ Manning Publications.  
> | CEO @ Phamola  
> | BGJUG-Bulgarian Java User Group Leader.  
> | Apache Maven Developer.  
> | Apache Jakarta PMC member.  
> | Jakarta Cactus Lead Developer.  
> | Codehaus Plexus Developer  
> | Blogger: [http://weblogs.java.net/blog/paranoiabla/](http://weblogs.java.net/blog/paranoiabla/)
> 
> * * *
> 
> Public PGP Key at:  
> [https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
> Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611
> 
> Attachments:
> 
> - result.txt
> 
> --  
> Regards, Petar!  
> Karlovo, Bulgaria.
> 
> * * *
> 
> | Author @ Manning Publications.  
> | CEO @ Phamola  
> | BGJUG-Bulgarian Java User Group Leader.  
> | Apache Maven Developer.  
> | Apache Jakarta PMC member.  
> | Jakarta Cactus Lead Developer.  
> | Codehaus Plexus Developer  
> | Blogger: [http://weblogs.java.net/blog/paranoiabla/](http://weblogs.java.net/blog/paranoiabla/)
> 
> * * *
> 
> Public PGP Key at:  
> [https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
> Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611
> 
> --  
> Regards, Petar!  
> Karlovo, Bulgaria.
> 
> * * *
> 
> | Author @ Manning Publications.  
> | CEO @ Phamola  
> | BGJUG-Bulgarian Java User Group Leader.  
> | Apache Maven Developer.  
> | Apache Jakarta PMC member.  
> | Jakarta Cactus Lead Developer.  
> | Codehaus Plexus Developer  
> | Blogger: [http://weblogs.java.net/blog/paranoiabla/](http://weblogs.java.net/blog/paranoiabla/)
> 
> * * *
> 
> Public PGP Key at:  
> [https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
> Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

--  
Regards, Petar!  
Karlovo, Bulgaria.

* * *

| Author @ Manning Publications.  
| CEO @ Phamola  
| BGJUG-Bulgarian Java User Group Leader.  
| Apache Maven Developer.  
| Apache Jakarta PMC member.  
| Jakarta Cactus Lead Developer.  
| Codehaus Plexus Developer  
| Blogger: [http://weblogs.java.net/blog/paranoiabla/](http://weblogs.java.net/blog/paranoiabla/)

* * *

Public PGP Key at:  
[https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [March 10, 2011, 2:02pm UTC](https://discuss.elastic.co/t/from-json-to-java-api-and-back/4064/12 "2011-03-10T14:02:30Z")

</div>

Make sure you create the builder _after_ you execute the search request. Also, you have the option of the builder outputting the json directly to the servlet output stream by using:

XContentBuilder builder = new XContentBuilder(XContentFactory.xContent(JSON), OutputStream);

p.s.

The above is kindda fugly, so I pushed simplified way to do that (in master):

XContetBuilder builder = XContentFactory.jsonBuilder(OutputStream)

On Thursday, March 10, 2011 at 3:48 PM, Petar Tahchiev wrote:

> check [gist:864108 · GitHub](https://gist.github.com/864108) for my code snippet
> 
> The expected JSON which should be createdshould look like this  
> {
> 
> - took: 9
> - timed\_out: false
> - \_shards: {  
> o total: 5  
> o successful: 5  
> o failed: 0  
> }  
> ..............  
> instead after this code is invoked a content which looks like this is returned:  
> 🙂  
> ò±µeryò¦©lteredòºmatch\_allò»»filteròº»ó»»explain#ó»¢´ook":1,"timed\_out":false,"\_shards":{"total":5,"successful":5,"failed":0},"hits":{"tot
> 
> so you see in the begining there are some strange non normally encoded  
> symbols ... in the toXContent method of XContentBuilder I see that the  
> method starts with adding a fields starting with TOOK... and etc  
> if (scrollId != null) {  
> builder.field(Fields.\_SCROLL\_ID, scrollId);  
> }  
> builder.field(Fields.TOOK, tookInMillis);  
> builder.field(Fields.TIMED\_OUT, timedOut());  
> (because the scroll\_id is null in my case ) so .. I dont know why at  
> the end before took somewhere this strangly encoded query is passed. (  
> it looks like my search query but the encoding is wrong ) )
> 
> Thanks with the help.
> 
> 2011/3/10 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com):
> 
> > You don't need to send any parameters. Can you _gist_ the code you use now?
> > 
> > On Thursday, March 10, 2011 at 10:55 AM, Petar Tahchiev wrote:
> > 
> > I guess I have to send some parameters other than EMPTY\_PARAMS, but I  
> > have no idea what exactly.
> > 
> > 2011/3/10 Petar Tahchiev [paranoiabla@gmail.com](mailto:paranoiabla@gmail.com):
> > 
> > OK, this time no exception is thrown, but instead I get again weird  
> > characters, like:  
> > "🙂  
> > ò±µeryò¦©lteredòºmatch\_allò»»filteròº»ó»»explain#â¦facetsò®¡meò´¥rmsò¦©eldCnameÆsizeÔ»ó»»»"took":4,"
> > 
> > which is the beginning of the String.
> > 
> > Do you have any idea what could cause that?
> > 
> > 2011/3/10 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com):
> > 
> > You need to wrap the toXContent call with startObject and endObject.  
> > Something like this:  
> > builder.startObject();  
> > response.toXContent(builder, request);  
> > builder.endObject();  
> > The stream is an internal serialization format, not a json one.
> > 
> > On Thursday, March 10, 2011 at 10:31 AM, Petar Tahchiev wrote:
> > 
> > Hi guys,
> > 
> > none of the suggestions seemed to work for me (or most likely Im doing  
> > something wrong).  
> > I first tried:
> > 
> > BytesStreamOutput stream = new BytesStreamOutput();
> > 
> > buildRequest(request).execute().actionGet().writeTo(stream);
> > 
> > stream.close();
> > 
> > String result = new String(stream.copiedByteArray());
> > 
> > System.out.println("!!" + result);
> > 
> > but the result I get is a semi-json containing some just random weird  
> > bytes (you can see the result.txt attached to this email)
> > 
> > Then I tried using:
> > 
> > XContentBuilder builder =  
> > XContentFactory.contentBuilder(XContentType.JSON);
> > 
> > buildRequest(request).execute().actionGet().toXContent(builder,  
> > ToXContent.EMPTY\_PARAMS);
> > 
> > String result = builder.string();
> > 
> > System.out.println("!!" + result);
> > 
> > but I get this exception
> > 
> > org.elasticsearch.common.jackson.JsonGenerationException: Can not  
> > write a field name, expecting a value  
> > org.elasticsearch.common.jackson.impl.JsonGeneratorBase.\_reportError(JsonGeneratorBase.java:481)  
> > org.elasticsearch.common.jackson.impl.Utf8Generator.writeFieldName(Utf8Generator.java:196)  
> > org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:71)  
> > org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:221)  
> > org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:357)  
> > org.elasticsearch.action.search.SearchResponse.toXContent(SearchResponse.java:229)
> > 
> > at this line  
> > buildRequest(request).execute().actionGet().toXContent(builder,  
> > ToXContent.EMPTY\_PARAMS);
> > 
> > Does anyone know what Im doing wrong?
> > 
> > 2011/3/9 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)
> > 
> > The question was about SearchResponse, not SearchRequest :). Parsing the  
> > search request from json is simple, just pass it using the  
> > SearchRequset#source(...) method.  
> > Note, you will still need to explicitly set the search type as its not part  
> > of the json "body" request.
> > 
> > On Wednesday, March 9, 2011 at 9:37 PM, Ridvan Gyundogan wrote:
> > 
> > @Shay  
> > There should be a way to parse json request to something like SearchRequest  
> > object, I think this is the second part of the question.  
> > When we send a curl search request you probably convert the json to java  
> > SearchRequest object?
> > 
> > On Wed, Mar 9, 2011 at 8:32 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)  
> > wrote:
> > 
> > SearchResponse implements the ToXContent interface, which means you can use  
> > XContentFactory to create a json builder, pass it to the toXContent method,  
> > and then get its binary / string representation. There is no way to parse a  
> > json to a SearchResponse.
> > 
> > On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:
> > 
> > Hi there,
> > 
> > Im using the Java API and I want to know what is the best way to  
> > convert the SearchResponse object into JSON format. Also If Im using  
> > the JSON API and I receive back a JSON response, is there a way to  
> > convert it to a SearchResponse.
> > 
> > Thank you for your time.
> > 
> > Cheers, Petar.
> > 
> > --  
> > Regards, Petar!  
> > Karlovo, Bulgaria.
> > 
> > * * *
> > 
> > | Author @ Manning Publications.  
> > | CEO @ Phamola  
> > | BGJUG-Bulgarian Java User Group Leader.  
> > | Apache Maven Developer.  
> > | Apache Jakarta PMC member.  
> > | Jakarta Cactus Lead Developer.  
> > | Codehaus Plexus Developer  
> > | Blogger: [http://weblogs.java.net/blog/paranoiabla/](http://weblogs.java.net/blog/paranoiabla/)
> > 
> > * * *
> > 
> > Public PGP Key at:  
> > [https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
> > Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611
> > 
> > Attachments:
> > 
> > - result.txt
> > 
> > --  
> > Regards, Petar!  
> > Karlovo, Bulgaria.
> > 
> > * * *
> > 
> > | Author @ Manning Publications.  
> > | CEO @ Phamola  
> > | BGJUG-Bulgarian Java User Group Leader.  
> > | Apache Maven Developer.  
> > | Apache Jakarta PMC member.  
> > | Jakarta Cactus Lead Developer.  
> > | Codehaus Plexus Developer  
> > | Blogger: [http://weblogs.java.net/blog/paranoiabla/](http://weblogs.java.net/blog/paranoiabla/)
> > 
> > * * *
> > 
> > Public PGP Key at:  
> > [https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
> > Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611
> > 
> > --  
> > Regards, Petar!  
> > Karlovo, Bulgaria.
> > 
> > * * *
> > 
> > | Author @ Manning Publications.  
> > | CEO @ Phamola  
> > | BGJUG-Bulgarian Java User Group Leader.  
> > | Apache Maven Developer.  
> > | Apache Jakarta PMC member.  
> > | Jakarta Cactus Lead Developer.  
> > | Codehaus Plexus Developer  
> > | Blogger: [http://weblogs.java.net/blog/paranoiabla/](http://weblogs.java.net/blog/paranoiabla/)
> > 
> > * * *
> > 
> > Public PGP Key at:  
> > [https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
> > Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611
> 
> --  
> Regards, Petar!  
> Karlovo, Bulgaria.
> 
> * * *
> 
> | Author @ Manning Publications.  
> | CEO @ Phamola  
> | BGJUG-Bulgarian Java User Group Leader.  
> | Apache Maven Developer.  
> | Apache Jakarta PMC member.  
> | Jakarta Cactus Lead Developer.  
> | Codehaus Plexus Developer  
> | Blogger: [http://weblogs.java.net/blog/paranoiabla/](http://weblogs.java.net/blog/paranoiabla/)
> 
> * * *
> 
> Public PGP Key at:  
> [https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
> Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

---

<div class="post-metadata">

**Author:** ![paranoiabla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paranoiabla/32/2160_2.png) [@paranoiabla](https://discuss.elastic.co/u/paranoiabla)\
**Post date:** [March 10, 2011, 2:10pm UTC](https://discuss.elastic.co/t/from-json-to-java-api-and-back/4064/13 "2011-03-10T14:10:26Z")

</div>

Hi Shay,

swaping the two lines seems to work good. I still think though that it  
is a really bad practice to  
hide such "functionality" with the code.

Thanks for your support.

Cheers, Petar.

2011/3/10 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com):

> Make sure you create the builder _after_ you execute the search request.  
> Also, you have the option of the builder outputting the json directly to the  
> servlet output stream by using:  
> XContentBuilder builder = new  
> XContentBuilder(XContentFactory.xContent(JSON), OutputStream);  
> p.s.  
> The above is kindda fugly, so I pushed simplified way to do that (in  
> master):  
> XContetBuilder builder = XContentFactory.jsonBuilder(OutputStream)
> 
> On Thursday, March 10, 2011 at 3:48 PM, Petar Tahchiev wrote:
> 
> check [gist:864108 · GitHub](https://gist.github.com/864108) for my code snippet
> 
> The expected JSON which should be createdshould look like this  
> {
> 
> - took: 9
> - timed\_out: false
> - \_shards: {  
> o total: 5  
> o successful: 5  
> o failed: 0  
> }  
> ..............  
> instead after this code is invoked a content which looks like this is  
> returned:  
> 🙂  
> 򄱵ery򇦩ltered򀺈match\_all򻻅filter򂺻󻻆explain#󻢴ook":1,"timed\_out":false,"\_shards":{"total":5,"successful":5,"failed":0},"hits":{"tot
> 
> so you see in the begining there are some strange non normally encoded  
> symbols ... in the toXContent method of XContentBuilder I see that the  
> method starts with adding a fields starting with TOOK... and etc  
> if (scrollId != null) {  
> builder.field(Fields.\_SCROLL\_ID, scrollId);  
> }  
> builder.field(Fields.TOOK, tookInMillis);  
> builder.field(Fields.TIMED\_OUT, timedOut());  
> (because the scroll\_id is null in my case ) so .. I dont know why at  
> the end before took somewhere this strangly encoded query is passed. (  
> it looks like my search query but the encoding is wrong ) )
> 
> Thanks with the help.
> 
> 2011/3/10 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com):
> 
> You don't need to send any parameters. Can you _gist_ the code you use now?
> 
> On Thursday, March 10, 2011 at 10:55 AM, Petar Tahchiev wrote:
> 
> I guess I have to send some parameters other than EMPTY\_PARAMS, but I  
> have no idea what exactly.
> 
> 2011/3/10 Petar Tahchiev [paranoiabla@gmail.com](mailto:paranoiabla@gmail.com):
> 
> OK, this time no exception is thrown, but instead I get again weird  
> characters, like:  
> "🙂  
> 򄱵ery򇦩ltered򀺈match\_all򻻅filter򂺻󻻆explain#…facets򃮡me򄴥rms򄦩eldCnameƒsizeԻ󻻻"took":4,"
> 
> which is the beginning of the String.
> 
> Do you have any idea what could cause that?
> 
> 2011/3/10 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com):
> 
> You need to wrap the toXContent call with startObject and endObject.  
> Something like this:  
> builder.startObject();  
> response.toXContent(builder, request);  
> builder.endObject();  
> The stream is an internal serialization format, not a json one.
> 
> On Thursday, March 10, 2011 at 10:31 AM, Petar Tahchiev wrote:
> 
> Hi guys,
> 
> none of the suggestions seemed to work for me (or most likely Im doing  
> something wrong).  
> I first tried:
> 
> ```
> BytesStreamOutput stream = new BytesStreamOutput();
> 
> buildRequest(request).execute().actionGet().writeTo(stream);
> 
> stream.close();
> 
> String result = new String(stream.copiedByteArray());
> 
> System.out.println("!!" + result);
> 
> ```
> 
> but the result I get is a semi-json containing some just random weird  
> bytes (you can see the result.txt attached to this email)
> 
> Then I tried using:
> 
> ```
> XContentBuilder builder =
> 
> ```
> 
> XContentFactory.contentBuilder(XContentType.JSON);
> 
> ```
> buildRequest(request).execute().actionGet().toXContent(builder,
> 
> ```
> 
> ToXContent.EMPTY\_PARAMS);
> 
> ```
> String result = builder.string();
> 
> System.out.println("!!" + result);
> 
> ```
> 
> but I get this exception
> 
> org.elasticsearch.common.jackson.JsonGenerationException: Can not  
> write a field name, expecting a value  
> org.elasticsearch.common.jackson.impl.JsonGeneratorBase.\_reportError(JsonGeneratorBase.java:481)  
> org.elasticsearch.common.jackson.impl.Utf8Generator.writeFieldName(Utf8Generator.java:196)  
> org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:71)  
> org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:221)  
> org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:357)  
> org.elasticsearch.action.search.SearchResponse.toXContent(SearchResponse.java:229)
> 
> at this line  
> buildRequest(request).execute().actionGet().toXContent(builder,  
> ToXContent.EMPTY\_PARAMS);
> 
> Does anyone know what Im doing wrong?
> 
> 2011/3/9 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)
> 
> The question was about SearchResponse, not SearchRequest :). Parsing the  
> search request from json is simple, just pass it using the  
> SearchRequset#source(...) method.  
> Note, you will still need to explicitly set the search type as its not part  
> of the json "body" request.
> 
> On Wednesday, March 9, 2011 at 9:37 PM, Ridvan Gyundogan wrote:
> 
> @Shay  
> There should be a way to parse json request to something like SearchRequest  
> object, I think this is the second part of the question.  
> When we send a curl search request you probably convert the json to java  
> SearchRequest object?
> 
> On Wed, Mar 9, 2011 at 8:32 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)  
> wrote:
> 
> SearchResponse implements the ToXContent interface, which means you can use  
> XContentFactory to create a json builder, pass it to the toXContent method,  
> and then get its binary / string representation. There is no way to parse a  
> json to a SearchResponse.
> 
> On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:
> 
> Hi there,
> 
> Im using the Java API and I want to know what is the best way to  
> convert the SearchResponse object into JSON format. Also If Im using  
> the JSON API and I receive back a JSON response, is there a way to  
> convert it to a SearchResponse.
> 
> Thank you for your time.
> 
> Cheers, Petar.
> 
> --  
> Regards, Petar!  
> Karlovo, Bulgaria.
> 
> * * *
> 
> | Author @ Manning Publications.  
> | CEO @ Phamola  
> | BGJUG-Bulgarian Java User Group Leader.  
> | Apache Maven Developer.  
> | Apache Jakarta PMC member.  
> | Jakarta Cactus Lead Developer.  
> | Codehaus Plexus Developer  
> | Blogger: [Oracle.com Outage](http://weblogs.java.net/blog/paranoiabla/)
> 
> * * *
> 
> Public PGP Key at:  
> [https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
> Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611
> 
> Attachments:
> 
> - result.txt
> 
> --  
> Regards, Petar!  
> Karlovo, Bulgaria.
> 
> * * *
> 
> | Author @ Manning Publications.  
> | CEO @ Phamola  
> | BGJUG-Bulgarian Java User Group Leader.  
> | Apache Maven Developer.  
> | Apache Jakarta PMC member.  
> | Jakarta Cactus Lead Developer.  
> | Codehaus Plexus Developer  
> | Blogger: [Oracle.com Outage](http://weblogs.java.net/blog/paranoiabla/)
> 
> * * *
> 
> Public PGP Key at:  
> [https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
> Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611
> 
> --  
> Regards, Petar!  
> Karlovo, Bulgaria.
> 
> * * *
> 
> | Author @ Manning Publications.  
> | CEO @ Phamola  
> | BGJUG-Bulgarian Java User Group Leader.  
> | Apache Maven Developer.  
> | Apache Jakarta PMC member.  
> | Jakarta Cactus Lead Developer.  
> | Codehaus Plexus Developer  
> | Blogger: [Oracle.com Outage](http://weblogs.java.net/blog/paranoiabla/)
> 
> * * *
> 
> Public PGP Key at:  
> [https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
> Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611
> 
> --  
> Regards, Petar!  
> Karlovo, Bulgaria.
> 
> * * *
> 
> | Author @ Manning Publications.  
> | CEO @ Phamola  
> | BGJUG-Bulgarian Java User Group Leader.  
> | Apache Maven Developer.  
> | Apache Jakarta PMC member.  
> | Jakarta Cactus Lead Developer.  
> | Codehaus Plexus Developer  
> | Blogger: [Oracle.com Outage](http://weblogs.java.net/blog/paranoiabla/)
> 
> * * *
> 
> Public PGP Key at:  
> [https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
> Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

--  
Regards, Petar!  
Karlovo, Bulgaria.

* * *

| Author @ Manning Publications.  
| CEO @ Phamola  
| BGJUG-Bulgarian Java User Group Leader.  
| Apache Maven Developer.  
| Apache Jakarta PMC member.  
| Jakarta Cactus Lead Developer.  
| Codehaus Plexus Developer  
| Blogger: [Oracle.com Outage](http://weblogs.java.net/blog/paranoiabla/)

* * *

Public PGP Key at:  
[https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [March 11, 2011, 12:58pm UTC](https://discuss.elastic.co/t/from-json-to-java-api-and-back/4064/14 "2011-03-11T12:58:00Z")

</div>

Hide what functionality with the code? And, make sure you don't convert it to String and then back to bytes, you can get the bytes out of it.  
On Thursday, March 10, 2011 at 4:10 PM, Petar Tahchiev wrote:

> Hi Shay,
> 
> swaping the two lines seems to work good. I still think though that it  
> is a really bad practice to  
> hide such "functionality" with the code.
> 
> Thanks for your support.
> 
> Cheers, Petar.
> 
> 2011/3/10 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com):
> 
> > Make sure you create the builder _after_ you execute the search request.  
> > Also, you have the option of the builder outputting the json directly to the  
> > servlet output stream by using:  
> > XContentBuilder builder = new  
> > XContentBuilder(XContentFactory.xContent(JSON), OutputStream);  
> > p.s.  
> > The above is kindda fugly, so I pushed simplified way to do that (in  
> > master):  
> > XContetBuilder builder = XContentFactory.jsonBuilder(OutputStream)
> > 
> > On Thursday, March 10, 2011 at 3:48 PM, Petar Tahchiev wrote:
> > 
> > check [gist:864108 · GitHub](https://gist.github.com/864108) for my code snippet
> > 
> > The expected JSON which should be createdshould look like this  
> > {
> > 
> > - took: 9
> > - timed\_out: false
> > - \_shards: {  
> > o total: 5  
> > o successful: 5  
> > o failed: 0  
> > }  
> > ..............  
> > instead after this code is invoked a content which looks like this is  
> > returned:  
> > 🙂  
> > ò±µeryò¦©lteredòºmatch\_allò»»filteròº»ó»»explain#ó»¢´ook":1,"timed\_out":false,"\_shards":{"total":5,"successful":5,"failed":0},"hits":{"tot
> > 
> > so you see in the begining there are some strange non normally encoded  
> > symbols ... in the toXContent method of XContentBuilder I see that the  
> > method starts with adding a fields starting with TOOK... and etc  
> > if (scrollId != null) {  
> > builder.field(Fields.\_SCROLL\_ID, scrollId);  
> > }  
> > builder.field(Fields.TOOK, tookInMillis);  
> > builder.field(Fields.TIMED\_OUT, timedOut());  
> > (because the scroll\_id is null in my case ) so .. I dont know why at  
> > the end before took somewhere this strangly encoded query is passed. (  
> > it looks like my search query but the encoding is wrong ) )
> > 
> > Thanks with the help.
> > 
> > 2011/3/10 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com):
> > 
> > You don't need to send any parameters. Can you _gist_ the code you use now?
> > 
> > On Thursday, March 10, 2011 at 10:55 AM, Petar Tahchiev wrote:
> > 
> > I guess I have to send some parameters other than EMPTY\_PARAMS, but I  
> > have no idea what exactly.
> > 
> > 2011/3/10 Petar Tahchiev [paranoiabla@gmail.com](mailto:paranoiabla@gmail.com):
> > 
> > OK, this time no exception is thrown, but instead I get again weird  
> > characters, like:  
> > "🙂  
> > ò±µeryò¦©lteredòºmatch\_allò»»filteròº»ó»»explain#â¦facetsò®¡meò´¥rmsò¦©eldCnameÆsizeÔ»ó»»»"took":4,"
> > 
> > which is the beginning of the String.
> > 
> > Do you have any idea what could cause that?
> > 
> > 2011/3/10 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com):
> > 
> > You need to wrap the toXContent call with startObject and endObject.  
> > Something like this:  
> > builder.startObject();  
> > response.toXContent(builder, request);  
> > builder.endObject();  
> > The stream is an internal serialization format, not a json one.
> > 
> > On Thursday, March 10, 2011 at 10:31 AM, Petar Tahchiev wrote:
> > 
> > Hi guys,
> > 
> > none of the suggestions seemed to work for me (or most likely Im doing  
> > something wrong).  
> > I first tried:
> > 
> > BytesStreamOutput stream = new BytesStreamOutput();
> > 
> > buildRequest(request).execute().actionGet().writeTo(stream);
> > 
> > stream.close();
> > 
> > String result = new String(stream.copiedByteArray());
> > 
> > System.out.println("!!" + result);
> > 
> > but the result I get is a semi-json containing some just random weird  
> > bytes (you can see the result.txt attached to this email)
> > 
> > Then I tried using:
> > 
> > XContentBuilder builder =  
> > XContentFactory.contentBuilder(XContentType.JSON);
> > 
> > buildRequest(request).execute().actionGet().toXContent(builder,  
> > ToXContent.EMPTY\_PARAMS);
> > 
> > String result = builder.string();
> > 
> > System.out.println("!!" + result);
> > 
> > but I get this exception
> > 
> > org.elasticsearch.common.jackson.JsonGenerationException: Can not  
> > write a field name, expecting a value  
> > org.elasticsearch.common.jackson.impl.JsonGeneratorBase.\_reportError(JsonGeneratorBase.java:481)  
> > org.elasticsearch.common.jackson.impl.Utf8Generator.writeFieldName(Utf8Generator.java:196)  
> > org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:71)  
> > org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:221)  
> > org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:357)  
> > org.elasticsearch.action.search.SearchResponse.toXContent(SearchResponse.java:229)
> > 
> > at this line  
> > buildRequest(request).execute().actionGet().toXContent(builder,  
> > ToXContent.EMPTY\_PARAMS);
> > 
> > Does anyone know what Im doing wrong?
> > 
> > 2011/3/9 Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)
> > 
> > The question was about SearchResponse, not SearchRequest :). Parsing the  
> > search request from json is simple, just pass it using the  
> > SearchRequset#source(...) method.  
> > Note, you will still need to explicitly set the search type as its not part  
> > of the json "body" request.
> > 
> > On Wednesday, March 9, 2011 at 9:37 PM, Ridvan Gyundogan wrote:
> > 
> > @Shay  
> > There should be a way to parse json request to something like SearchRequest  
> > object, I think this is the second part of the question.  
> > When we send a curl search request you probably convert the json to java  
> > SearchRequest object?
> > 
> > On Wed, Mar 9, 2011 at 8:32 PM, Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)  
> > wrote:
> > 
> > SearchResponse implements the ToXContent interface, which means you can use  
> > XContentFactory to create a json builder, pass it to the toXContent method,  
> > and then get its binary / string representation. There is no way to parse a  
> > json to a SearchResponse.
> > 
> > On Wednesday, March 9, 2011 at 3:54 PM, paranoiabla wrote:
> > 
> > Hi there,
> > 
> > Im using the Java API and I want to know what is the best way to  
> > convert the SearchResponse object into JSON format. Also If Im using  
> > the JSON API and I receive back a JSON response, is there a way to  
> > convert it to a SearchResponse.
> > 
> > Thank you for your time.
> > 
> > Cheers, Petar.
> > 
> > --  
> > Regards, Petar!  
> > Karlovo, Bulgaria.
> > 
> > * * *
> > 
> > | Author @ Manning Publications.  
> > | CEO @ Phamola  
> > | BGJUG-Bulgarian Java User Group Leader.  
> > | Apache Maven Developer.  
> > | Apache Jakarta PMC member.  
> > | Jakarta Cactus Lead Developer.  
> > | Codehaus Plexus Developer  
> > | Blogger: [http://weblogs.java.net/blog/paranoiabla/](http://weblogs.java.net/blog/paranoiabla/)
> > 
> > * * *
> > 
> > Public PGP Key at:  
> > [https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
> > Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611
> > 
> > Attachments:
> > 
> > - result.txt
> > 
> > --  
> > Regards, Petar!  
> > Karlovo, Bulgaria.
> > 
> > * * *
> > 
> > | Author @ Manning Publications.  
> > | CEO @ Phamola  
> > | BGJUG-Bulgarian Java User Group Leader.  
> > | Apache Maven Developer.  
> > | Apache Jakarta PMC member.  
> > | Jakarta Cactus Lead Developer.  
> > | Codehaus Plexus Developer  
> > | Blogger: [http://weblogs.java.net/blog/paranoiabla/](http://weblogs.java.net/blog/paranoiabla/)
> > 
> > * * *
> > 
> > Public PGP Key at:  
> > [https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
> > Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611
> > 
> > --  
> > Regards, Petar!  
> > Karlovo, Bulgaria.
> > 
> > * * *
> > 
> > | Author @ Manning Publications.  
> > | CEO @ Phamola  
> > | BGJUG-Bulgarian Java User Group Leader.  
> > | Apache Maven Developer.  
> > | Apache Jakarta PMC member.  
> > | Jakarta Cactus Lead Developer.  
> > | Codehaus Plexus Developer  
> > | Blogger: [http://weblogs.java.net/blog/paranoiabla/](http://weblogs.java.net/blog/paranoiabla/)
> > 
> > * * *
> > 
> > Public PGP Key at:  
> > [https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
> > Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611
> > 
> > --  
> > Regards, Petar!  
> > Karlovo, Bulgaria.
> > 
> > * * *
> > 
> > | Author @ Manning Publications.  
> > | CEO @ Phamola  
> > | BGJUG-Bulgarian Java User Group Leader.  
> > | Apache Maven Developer.  
> > | Apache Jakarta PMC member.  
> > | Jakarta Cactus Lead Developer.  
> > | Codehaus Plexus Developer  
> > | Blogger: [http://weblogs.java.net/blog/paranoiabla/](http://weblogs.java.net/blog/paranoiabla/)
> > 
> > * * *
> > 
> > Public PGP Key at:  
> > [https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
> > Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611
> 
> --  
> Regards, Petar!  
> Karlovo, Bulgaria.
> 
> * * *
> 
> | Author @ Manning Publications.  
> | CEO @ Phamola  
> | BGJUG-Bulgarian Java User Group Leader.  
> | Apache Maven Developer.  
> | Apache Jakarta PMC member.  
> | Jakarta Cactus Lead Developer.  
> | Codehaus Plexus Developer  
> | Blogger: [http://weblogs.java.net/blog/paranoiabla/](http://weblogs.java.net/blog/paranoiabla/)
> 
> * * *
> 
> Public PGP Key at:  
> [https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611](https://keyserver1.pgp.com/vkd/DownloadKey.event?keyid=0x19658550C3110611)  
> Key Fingerprint: A369 A7EE 61BC 93A3 CDFF 55A5 1965 8550 C311 0611

---

<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 6, 2017, 4:10am UTC](https://discuss.elastic.co/t/from-json-to-java-api-and-back/4064/15 "2017-07-06T04:10:18Z")

</div>


