# Converting a FacetBuilder to Json

**URL:** <https://discuss.elastic.co/t/converting-a-facetbuilder-to-json/5485>\
**Category:** Elasticsearch\
**Created:** [September 30, 2011, 6:11am UTC](https://discuss.elastic.co/t/converting-a-facetbuilder-to-json/5485 "2011-09-30T06:11:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![leonm\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leonm_2/32/3118_2.png) [@leonm\_2](https://discuss.elastic.co/u/leonm_2)\
**Post date:** [September 30, 2011, 6:11am UTC](https://discuss.elastic.co/t/converting-a-facetbuilder-to-json/5485/1 "2011-09-30T06:11:48Z")

</div>

I'm trying to write a little Unit Test to check that we setting up our  
objects correctly. To do this I'm setting up a FacetBuilder and  
convert it to Json that from where I can assert its correctness.

However, when I try to convert the FacetBuilder to Json I get an  
Exception:

TermsFacetBuilder facetBuilder =  
FacetBuilders.termsFacet("name").field("field");  
String jsonString =  
facetBuilder.toXContent(XContentFactory.safeJsonBuilder(),  
EMPTY\_PARAMS).string();

org.elasticsearch.common.jackson.JsonGenerationException: Can not  
write a field name, expecting a value  
at  
org.elasticsearch.common.jackson.impl.JsonGeneratorBase.\_reportError(JsonGeneratorBase.java:  
480)  
at  
org.elasticsearch.common.jackson.impl.Utf8Generator.writeFieldName(Utf8Generator.java:  
270)  
at  
org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:  
70)  
at  
org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:  
253)  
at  
org.elasticsearch.common.xcontent.XContentBuilder.startObject(XContentBuilder.java:  
139)  
at  
org.elasticsearch.search.facet.terms.TermsFacetBuilder.toXContent(TermsFacetBuilder.java:  
211)

I tried both versions 0.17.4 and 0.17.7 with pretty consistent results  
(StackTrace is for 0.17.7)

I fired up a debugger and stepped through the code and there is  
nothing that is null or otherwise broken.

---

<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:** [October 2, 2011, 8:52am UTC](https://discuss.elastic.co/t/converting-a-facetbuilder-to-json/5485/2 "2011-10-02T08:52:16Z")

</div>

If you just need to print the facet, you need to wrap it with startObject()  
({), and endObject(}) to make it in a formal json structure.

On Fri, Sep 30, 2011 at 9:11 AM, leonm [leon.messerschmidt@gmail.com](mailto:leon.messerschmidt@gmail.com) wrote:

> I'm trying to write a little Unit Test to check that we setting up our  
> objects correctly. To do this I'm setting up a FacetBuilder and  
> convert it to Json that from where I can assert its correctness.
> 
> However, when I try to convert the FacetBuilder to Json I get an  
> Exception:
> 
> TermsFacetBuilder facetBuilder =  
> FacetBuilders.termsFacet("name").field("field");  
> String jsonString =  
> facetBuilder.toXContent(XContentFactory.safeJsonBuilder(),  
> EMPTY\_PARAMS).string();
> 
> org.elasticsearch.common.jackson.JsonGenerationException: Can not  
> write a field name, expecting a value  
> at
> 
> org.elasticsearch.common.jackson.impl.JsonGeneratorBase.\_reportError(JsonGeneratorBase.java:  
> 480)  
> at
> 
> org.elasticsearch.common.jackson.impl.Utf8Generator.writeFieldName(Utf8Generator.java:  
> 270)  
> at
> 
> org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:  
> 70)  
> at
> 
> org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.java:  
> 253)  
> at
> 
> org.elasticsearch.common.xcontent.XContentBuilder.startObject(XContentBuilder.java:  
> 139)  
> at
> 
> org.elasticsearch.search.facet.terms.TermsFacetBuilder.toXContent(TermsFacetBuilder.java:  
> 211)
> 
> I tried both versions 0.17.4 and 0.17.7 with pretty consistent results  
> (StackTrace is for 0.17.7)
> 
> I fired up a debugger and stepped through the code and there is  
> nothing that is null or otherwise broken.

---

<div class="post-metadata">

**Author:** ![leonm\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leonm_2/32/3118_2.png) [@leonm\_2](https://discuss.elastic.co/u/leonm_2)\
**Post date:** [October 2, 2011, 11:19pm UTC](https://discuss.elastic.co/t/converting-a-facetbuilder-to-json/5485/3 "2011-10-02T23:19:57Z")

</div>

Thank you, that works!

One more question if you don't mind: What confused me was that a  
Filter Builder happily creates json without the startObject() but a  
Facet Builder doesn't. Is there something that I can use to predict  
when it is required?

On Oct 2, 6:52 pm, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> If you just need to print the facet, you need to wrap it with startObject()  
> ({), and endObject(}) to make it in a formal json structure.
> 
> On Fri, Sep 30, 2011 at 9:11 AM, leonm [leon.messerschm...@gmail.com](mailto:leon.messerschm...@gmail.com) wrote:
> 
> > I'm trying to write a little Unit Test to check that we setting up our  
> > objects correctly. To do this I'm setting up a FacetBuilder and  
> > convert it to Json that from where I can assert its correctness.
> 
> > However, when I try to convert the FacetBuilder to Json I get an  
> > Exception:
> 
> > TermsFacetBuilder facetBuilder =  
> > FacetBuilders.termsFacet("name").field("field");  
> > String jsonString =  
> > facetBuilder.toXContent(XContentFactory.safeJsonBuilder(),  
> > EMPTY\_PARAMS).string();
> 
> > org.elasticsearch.common.jackson.JsonGenerationException: Can not  
> > write a field name, expecting a value  
> > at
> 
> > org.elasticsearch.common.jackson.impl.JsonGeneratorBase.\_reportError(JsonGe neratorBase.java:  
> > 480)  
> > at
> 
> > org.elasticsearch.common.jackson.impl.Utf8Generator.writeFieldName(Utf8Gene rator.java:  
> > 270)  
> > at
> 
> > org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName (JsonXContentGenerator.java:  
> > 70)  
> > at
> 
> > org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.jav a:  
> > 253)  
> > at
> 
> > org.elasticsearch.common.xcontent.XContentBuilder.startObject(XContentBuild er.java:  
> > 139)  
> > at
> 
> > org.elasticsearch.search.facet.terms.TermsFacetBuilder.toXContent(TermsFace tBuilder.java:  
> > 211)
> 
> > I tried both versions 0.17.4 and 0.17.7 with pretty consistent results  
> > (StackTrace is for 0.17.7)
> 
> > I fired up a debugger and stepped through the code and there is  
> > nothing that is null or otherwise broken.

---

<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:** [October 3, 2011, 9:33am UTC](https://discuss.elastic.co/t/converting-a-facetbuilder-to-json/5485/4 "2011-10-03T09:33:21Z")

</div>

Its because of the expected stage at which those different elements (query,  
filter, facet) are used within the search request. Sometimes, they need to  
be in the form of { ... } (like filters), and sometimes, they need to be  
outputted like "name" : { ... } (in case of facets), to be pluggable into  
the the full search request.

On Mon, Oct 3, 2011 at 1:19 AM, leonm [leon.messerschmidt@gmail.com](mailto:leon.messerschmidt@gmail.com) wrote:

> Thank you, that works!
> 
> One more question if you don't mind: What confused me was that a  
> Filter Builder happily creates json without the startObject() but a  
> Facet Builder doesn't. Is there something that I can use to predict  
> when it is required?
> 
> On Oct 2, 6:52 pm, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:
> 
> > If you just need to print the facet, you need to wrap it with  
> > startObject()  
> > ({), and endObject(}) to make it in a formal json structure.
> > 
> > On Fri, Sep 30, 2011 at 9:11 AM, leonm [leon.messerschm...@gmail.com](mailto:leon.messerschm...@gmail.com)  
> > wrote:
> > 
> > > I'm trying to write a little Unit Test to check that we setting up our  
> > > objects correctly. To do this I'm setting up a FacetBuilder and  
> > > convert it to Json that from where I can assert its correctness.
> > 
> > > However, when I try to convert the FacetBuilder to Json I get an  
> > > Exception:
> > 
> > > TermsFacetBuilder facetBuilder =  
> > > FacetBuilders.termsFacet("name").field("field");  
> > > String jsonString =  
> > > facetBuilder.toXContent(XContentFactory.safeJsonBuilder(),  
> > > EMPTY\_PARAMS).string();
> > 
> > > org.elasticsearch.common.jackson.JsonGenerationException: Can not  
> > > write a field name, expecting a value  
> > > at
> > 
> > >
> 
> org.elasticsearch.common.jackson.impl.JsonGeneratorBase.\_reportError(JsonGe  
> neratorBase.java:
> 
> > > 1. at
> > 
> > >
> 
> org.elasticsearch.common.jackson.impl.Utf8Generator.writeFieldName(Utf8Gene  
> rator.java:
> 
> > > 1. at
> > 
> > >
> 
> org.elasticsearch.common.xcontent.json.JsonXContentGenerator.writeFieldName  
> (JsonXContentGenerator.java:
> 
> > > 1. at
> > 
> > >
> 
> org.elasticsearch.common.xcontent.XContentBuilder.field(XContentBuilder.jav  
> a:
> 
> > > 1. at
> > 
> > >
> 
> org.elasticsearch.common.xcontent.XContentBuilder.startObject(XContentBuild  
> er.java:
> 
> > > 1. at
> > 
> > >
> 
> org.elasticsearch.search.facet.terms.TermsFacetBuilder.toXContent(TermsFace  
> tBuilder.java:
> 
> > > 1.
> > 
> > > I tried both versions 0.17.4 and 0.17.7 with pretty consistent results  
> > > (StackTrace is for 0.17.7)
> > 
> > > I fired up a debugger and stepped through the code and there is  
> > > nothing that is null or otherwise broken.

---

<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, 3:53am UTC](https://discuss.elastic.co/t/converting-a-facetbuilder-to-json/5485/5 "2017-07-06T03:53:00Z")

</div>


