# JsonGenerationException: Can not write a field name, expecting a value

**URL:** <https://discuss.elastic.co/t/jsongenerationexception-can-not-write-a-field-name-expecting-a-value/10616>\
**Category:** Elasticsearch\
**Created:** [February 4, 2013, 2:42pm UTC](https://discuss.elastic.co/t/jsongenerationexception-can-not-write-a-field-name-expecting-a-value/10616 "2013-02-04T14:42:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eugene\_Strokin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eugene_strokin/32/1238_2.png) [@Eugene\_Strokin](https://discuss.elastic.co/u/Eugene_Strokin)\
**Post date:** [February 4, 2013, 2:42pm UTC](https://discuss.elastic.co/t/jsongenerationexception-can-not-write-a-field-name-expecting-a-value/10616/1 "2013-02-04T14:42:08Z")

</div>

ToXContent response=///////in debugger it shows:  
{  
"took" : 3,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"failed" : 0  
},  
"hits" : {  
"total" : 0,  
"max\_score" : null,  
"hits" : []  
}  
}  
////////

XContentBuilder builder=XContentFactory.contentBuilder(XContentType.JSON);  
response.toXContent(builder, ToXContent.EMPTY\_PARAMS);

The last line throws the exception:

org.elasticsearch.common.jackson.core.JsonGenerationException: Can notwrite a field name  
, expecting a value  
at org.elasticsearch.common.jackson.core.base.GeneratorBase.\_reportError(  
GeneratorBase.java:424)  
at org.elasticsearch.common.jackson.core.json.UTF8JsonGenerator.  
writeFieldName(UTF8JsonGenerator.java:189)  
at org.elasticsearch.common.xcontent.json.JsonXContentGenerator.  
writeFieldName(JsonXContentGenerator.java:79)  
at org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.  
java:239)  
at org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.  
java:406)  
at org.elasticsearch.action.search.SearchResponse.toXContent(SearchResponse  
.java:249)

I'm not sure what could possibly be wrong here.

Could somebody help?

Thank you,

Eugene

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 4, 2013, 3:04pm UTC](https://discuss.elastic.co/t/jsongenerationexception-can-not-write-a-field-name-expecting-a-value/10616/2 "2013-02-04T15:04:47Z")

</div>

Hi Eugene

What are you trying to achieve here?  
I don't get it.

Are you trying to build your own JSon document or just want to extract information from response?

Le 4 févr. 2013 à 15:42, Eugene Strokin [eugene@strokin.info](mailto:eugene@strokin.info) a écrit :

> ToXContent response=///////in debugger it shows:  
> {  
> "took" : 3,  
> "timed\_out" : false,  
> "\_shards" : {  
> "total" : 5,  
> "successful" : 5,  
> "failed" : 0  
> },  
> "hits" : {  
> "total" : 0,  
> "max\_score" : null,  
> "hits" :   
> }  
> }  
> ////////
> 
> XContentBuilder builder=XContentFactory.contentBuilder(XContentType.JSON);  
> response.toXContent(builder, ToXContent.EMPTY\_PARAMS);
> 
> The last line throws the exception:
> 
> org.elasticsearch.common.jackson.core.JsonGenerationException: Can not write a field name, expecting a value  
> at org.elasticsearch.common.jackson.core.base.GeneratorBase.\_reportError(GeneratorBase.java:424)  
> at org.elasticsearch.common.jackson.core.json.UTF8JsonGenerator.writeFieldName(UTF8JsonGenerator.java:189)  
> at org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:79)  
> at org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:239)  
> at org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:406)  
> at org.elasticsearch.action.search.SearchResponse.toXContent(SearchResponse.java:249)
> 
> I'm not sure what could possibly be wrong here.  
> Could somebody help?  
> Thank you,  
> Eugene
> 
> --  
> You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Eugene\_Strokin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eugene_strokin/32/1238_2.png) [@Eugene\_Strokin](https://discuss.elastic.co/u/Eugene_Strokin)\
**Post date:** [February 4, 2013, 3:13pm UTC](https://discuss.elastic.co/t/jsongenerationexception-can-not-write-a-field-name-expecting-a-value/10616/3 "2013-02-04T15:13:24Z")

</div>

All I need, is to get response from ES into some readable format. I guess  
the next line of code should be something like this:

builder.string()  
But I couldn't even get there because of the exception.

If there is other way reading data from response, please advice. At the  
end, I need just a POJO populated with data from the response.

Thank you,

Eugene

On Monday, February 4, 2013 10:04:47 AM UTC-5, David Pilato wrote:

> Hi Eugene
> 
> What are you trying to achieve here?  
> I don't get it.
> 
> Are you trying to build your own JSon document or just want to extract  
> information from response?
> 
> Le 4 févr. 2013 à 15:42, Eugene Strokin \<eug...@strokin.info \<javascript:\>\>  
> a écrit :
> 
> ToXContent response=///////in debugger it shows:  
> {  
> "took" : 3,  
> "timed\_out" : false,  
> "\_shards" : {  
> "total" : 5,  
> "successful" : 5,  
> "failed" : 0  
> },  
> "hits" : {  
> "total" : 0,  
> "max\_score" : null,  
> "hits" :   
> }  
> }  
> ////////
> 
> XContentBuilder builder=XContentFactory.contentBuilder(XContentType.JSON);  
> response.toXContent(builder, ToXContent.EMPTY\_PARAMS);
> 
> The last line throws the exception:
> 
> org.elasticsearch.common.jackson.core.JsonGenerationException: Can notwrite a field name  
> , expecting a value  
> at org.elasticsearch.common.jackson.core.base.GeneratorBase.\_reportError(  
> GeneratorBase.java:424)  
> at org.elasticsearch.common.jackson.core.json.UTF8JsonGenerator.  
> writeFieldName(UTF8JsonGenerator.java:189)  
> at org.elasticsearch.common.xcontent.json.JsonXContentGenerator.  
> writeFieldName(JsonXContentGenerator.java:79)  
> at org.elasticsearch.common.xcontent.XContentBuilder.field(  
> XContentBuilder.java:239)  
> at org.elasticsearch.common.xcontent.XContentBuilder.field(  
> XContentBuilder.java:406)  
> at org.elasticsearch.action.search.SearchResponse.toXContent(  
> SearchResponse.java:249)
> 
> I'm not sure what could possibly be wrong here.
> 
> Could somebody help?
> 
> Thank you,
> 
> Eugene
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [February 4, 2013, 3:29pm UTC](https://discuss.elastic.co/t/jsongenerationexception-can-not-write-a-field-name-expecting-a-value/10616/4 "2013-02-04T15:29:14Z")

</div>

Hi,

please, use the Java API for ES search responses, see SearchResponse  
class. You do not need to parse HTTP REST JSON by yourself.

If you feel you must fiddle with HTTP REST JSON and map it to Java  
Pojos, use a JSON XContentParser. It's a oneliner and works like this

String json = ...  
Map\<String,Object\> map =  
JsonXContent.jsonXContent.createParser(json).mapAndClose();

The error ou received is a hint that you must start en empty builder  
with the startObject() or startArray() method.

Jörg

Am 04.02.13 15:42, schrieb Eugene Strokin:

> |  
> ToXContentresponse=///////in debugger it shows:  
> {  
> "took":3,  
> "timed\_out":false,  
> "\_shards":{  
> "total":5,  
> "successful":5,  
> "failed":0  
> },  
> "hits":{  
> "total":0,  
> "max\_score":null,  
> "hits":  
> }  
> }  
> ////////
> 
> XContentBuilderbuilder=XContentFactory.contentBuilder(XContentType.JSON);  
> response.toXContent(builder,ToXContent.EMPTY\_PARAMS);  
> |
> 
> The last line throws the exception:
> 
> |  
> org.elasticsearch.common.jackson.core.JsonGenerationException:Cannotwrite  
> a field name,expecting a value  
> at  
> org.elasticsearch.common.jackson.core.base.GeneratorBase.\_reportError(GeneratorBase.java:424)  
> at  
> org.elasticsearch.common.jackson.core.json.UTF8JsonGenerator.writeFieldName(UTF8JsonGenerator.java:189)  
> at  
> org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:79)  
> at  
> org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:239)  
> at  
> org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:406)  
> at  
> org.elasticsearch.action.search.SearchResponse.toXContent(SearchResponse.java:249)  
> |
> 
> I'm not sure what could possibly be wrong here.
> 
> Could somebody help?
> 
> Thank you,
> 
> Eugene
> 
> --  
> You received this message because you are subscribed to the Google  
> Groups "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send  
> an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 4, 2013, 3:38pm UTC](https://discuss.elastic.co/t/jsongenerationexception-can-not-write-a-field-name-expecting-a-value/10616/5 "2013-02-04T15:38:40Z")

</div>

Here is an example on how I proceed to read back hits and convert them to beans: [https://github.com/elasticsearchfr/hands-on/blob/answers/src/test/java/org/elasticsearchfr/handson/ex2/SearchTest.java#L267](https://github.com/elasticsearchfr/hands-on/blob/answers/src/test/java/org/elasticsearchfr/handson/ex2/SearchTest.java#L267)

HTH  
David.

Le 4 févr. 2013 à 16:13, Eugene Strokin [eugene@strokin.info](mailto:eugene@strokin.info) a écrit :

> All I need, is to get response from ES into some readable format. I guess the next line of code should be something like this:
> 
> builder.string()  
> But I couldn't even get there because of the exception.
> 
> If there is other way reading data from response, please advice. At the end, I need just a POJO populated with data from the response.  
> Thank you,  
> Eugene
> 
> On Monday, February 4, 2013 10:04:47 AM UTC-5, David Pilato wrote:  
> Hi Eugene
> 
> What are you trying to achieve here?  
> I don't get it.
> 
> Are you trying to build your own JSon document or just want to extract information from response?
> 
> Le 4 févr. 2013 à 15:42, Eugene Strokin [eug...@strokin.info](mailto:eug...@strokin.info) a écrit :
> 
> > ToXContent response=///////in debugger it shows:  
> > {  
> > "took" : 3,  
> > "timed\_out" : false,  
> > "\_shards" : {  
> > "total" : 5,  
> > "successful" : 5,  
> > "failed" : 0  
> > },  
> > "hits" : {  
> > "total" : 0,  
> > "max\_score" : null,  
> > "hits" :   
> > }  
> > }  
> > ////////
> > 
> > XContentBuilder builder=XContentFactory.contentBuilder(XContentType.JSON);  
> > response.toXContent(builder, ToXContent.EMPTY\_PARAMS);
> > 
> > The last line throws the exception:
> > 
> > org.elasticsearch.common.jackson.core.JsonGenerationException: Can not write a field name, expecting a value  
> > at org.elasticsearch.common.jackson.core.base.GeneratorBase.\_reportError(GeneratorBase.java:424)  
> > at org.elasticsearch.common.jackson.core.json.UTF8JsonGenerator.writeFieldName(UTF8JsonGenerator.java:189)  
> > at org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:79)  
> > at org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:239)  
> > at org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:406)  
> > at org.elasticsearch.action.search.SearchResponse.toXContent(SearchResponse.java:249)
> > 
> > I'm not sure what could possibly be wrong here.  
> > Could somebody help?  
> > Thank you,  
> > Eugene
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Eugene\_Strokin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eugene_strokin/32/1238_2.png) [@Eugene\_Strokin](https://discuss.elastic.co/u/Eugene_Strokin)\
**Post date:** [February 4, 2013, 5:33pm UTC](https://discuss.elastic.co/t/jsongenerationexception-can-not-write-a-field-name-expecting-a-value/10616/6 "2013-02-04T17:33:26Z")

</div>

This is exactly what I need. Thank you David.

I've shared an Utility for populating data from ES responses and generating  
ES source for indexing.  
May be it would help someone:

> <https://gist.github.com/strokine/4708178>
>
> There are more than three files. show original

On Monday, February 4, 2013 10:38:40 AM UTC-5, David Pilato wrote:

> Here is an example on how I proceed to read back hits and convert them to  
> beans:  
> [https://github.com/elasticsearchfr/hands-on/blob/answers/src/test/java/org/elasticsearchfr/handson/ex2/SearchTest.java#L267](https://github.com/elasticsearchfr/hands-on/blob/answers/src/test/java/org/elasticsearchfr/handson/ex2/SearchTest.java#L267)
> 
> HTH  
> David.
> 
> Le 4 févr. 2013 à 16:13, Eugene Strokin \<eug...@strokin.info \<javascript:\>\>  
> a écrit :
> 
> All I need, is to get response from ES into some readable format. I guess  
> the next line of code should be something like this:
> 
> builder.string()  
> But I couldn't even get there because of the exception.
> 
> If there is other way reading data from response, please advice. At the  
> end, I need just a POJO populated with data from the response.
> 
> Thank you,
> 
> Eugene
> 
> On Monday, February 4, 2013 10:04:47 AM UTC-5, David Pilato wrote:
> 
> > Hi Eugene
> > 
> > What are you trying to achieve here?  
> > I don't get it.
> > 
> > Are you trying to build your own JSon document or just want to extract  
> > information from response?
> > 
> > Le 4 févr. 2013 à 15:42, Eugene Strokin [eug...@strokin.info](mailto:eug...@strokin.info) a écrit :
> > 
> > ToXContent response=///////in debugger it shows:  
> > {  
> > "took" : 3,  
> > "timed\_out" : false,  
> > "\_shards" : {  
> > "total" : 5,  
> > "successful" : 5,  
> > "failed" : 0  
> > },  
> > "hits" : {  
> > "total" : 0,  
> > "max\_score" : null,  
> > "hits" :   
> > }  
> > }  
> > ////////
> > 
> > XContentBuilder builder=XContentFactory.contentBuilder(XContentType.JSON  
> > );  
> > response.toXContent(builder, ToXContent.EMPTY\_PARAMS);
> > 
> > The last line throws the exception:
> > 
> > org.elasticsearch.common.jackson.core.JsonGenerationException: Can notwrite a field name  
> > , expecting a value  
> > at org.elasticsearch.common.jackson.core.base.GeneratorBase.\_reportError  
> > (GeneratorBase.java:424)  
> > at org.elasticsearch.common.jackson.core.json.UTF8JsonGenerator.  
> > writeFieldName(UTF8JsonGenerator.java:189)  
> > at org.elasticsearch.common.xcontent.json.JsonXContentGenerator.  
> > writeFieldName(JsonXContentGenerator.java:79)  
> > at org.elasticsearch.common.xcontent.XContentBuilder.field(  
> > XContentBuilder.java:239)  
> > at org.elasticsearch.common.xcontent.XContentBuilder.field(  
> > XContentBuilder.java:406)  
> > at org.elasticsearch.action.search.SearchResponse.toXContent(  
> > SearchResponse.java:249)
> > 
> > I'm not sure what could possibly be wrong here.
> > 
> > Could somebody help?
> > 
> > Thank you,
> > 
> > Eugene
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:53am UTC](https://discuss.elastic.co/t/jsongenerationexception-can-not-write-a-field-name-expecting-a-value/10616/7 "2017-07-06T02:53:05Z")

</div>


