# Invalid JSON from XContentBuilder

**URL:** <https://discuss.elastic.co/t/invalid-json-from-xcontentbuilder/22329>\
**Category:** Elasticsearch\
**Created:** [February 23, 2015, 12:57pm UTC](https://discuss.elastic.co/t/invalid-json-from-xcontentbuilder/22329 "2015-02-23T12:57:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Klausen\_Schaefersinh](https://avatars.discourse-cdn.com/v4/letter/k/d2c977/32.png) [@Klausen\_Schaefersinh](https://discuss.elastic.co/u/Klausen_Schaefersinh)\
**Post date:** [February 23, 2015, 12:57pm UTC](https://discuss.elastic.co/t/invalid-json-from-xcontentbuilder/22329/1 "2015-02-23T12:57:29Z")

</div>

Hi,

I am trying to get the term vector for an document in Java. The problem is, that the call returns an object where the realy payload is not accessible through the API. Therefore I was now trying to go around and use the toXContent() method and generate a josn and use this to acess the data. However the JSON String generated by the XContentBuilder is not valid. Before valid json comes, I have some not valid string like \_id, \_type etc. It seems the meatinfo is somehow prefixed to the real json. How can I solve this issue?

Here is an example of the returned json:

"\_index""content","\_type" "content","\_id"  
"[http://www.page.com/a/1.html","\_version](http://www.page.com/a/1.html%22,%22_version)" 1,"found" true,"term\_vectors"  
{"main":{"field\_statistics":{"sum\_doc\_freq":4,"doc\_count":1,"sum\_ttf":4},"terms":{"döner":{"doc\_freq":1,"ttf":1,"term\_freq":1,"tokens":[{"position":3,"start\_offset":16,"end\_offset":21,"payload":"PEFMUEhBTlVNPg=="}]},"einen":{"doc\_freq":1,"ttf":1,"term\_freq":1,"tokens":[{"position":2,"start\_offset":10,"end\_offset":15,"payload":"PEFMUEhBTlVNPg=="}]},"ist":{"doc\_freq":1,"ttf":1,"term\_freq":1,"tokens":[{"position":1,"start\_offset":6,"end\_offset":9,"payload":"PEFMUEhBTlVNPg=="}]},"klaus":{"doc\_freq":1,"ttf":1,"term\_freq":1,"tokens":[{"position":0,"start\_offset":0,"end\_offset":5,"payload":"PEFMUEhBTlVNPg=="}]}}}

And here is my code:

final TermVectorResponse termVectorResponse = getClient().termVector(new TermVectorRequest()

```
  .index(this.index)

  .type(this.type)

  .id(id)

  .termStatistics(true)

  .selectedFields(new String[]{"main", "abs", "title"}))

  .actionGet();

```

XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);

termVectorResponse.toXContent(builder, null);

Cheers,

Klaus

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/df00a133-ce13-4488-ba21-e6b40b654e4d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/df00a133-ce13-4488-ba21-e6b40b654e4d%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Klausen\_Schaefersinh](https://avatars.discourse-cdn.com/v4/letter/k/d2c977/32.png) [@Klausen\_Schaefersinh](https://discuss.elastic.co/u/Klausen_Schaefersinh)\
**Post date:** [February 23, 2015, 1:03pm UTC](https://discuss.elastic.co/t/invalid-json-from-xcontentbuilder/22329/2 "2015-02-23T13:03:02Z")

</div>

Hi,

I found the solution:

builder.startObject();

termVectorResponse.toXContent(builder, ToXContent.EMPTY\_PARAMS);

builder.endObject();

You have to first create an object in the builder.

Cheers,

KLaus

On Monday, February 23, 2015 at 1:57:29 PM UTC+1, Klausen Schaefersinho  
wrote:

> Hi,
> 
> I am trying to get the term vector for an document in Java. The problem is, that the call returns an object where the realy payload is not accessible through the API. Therefore I was now trying to go around and use the toXContent() method and generate a josn and use this to acess the data. However the JSON String generated by the XContentBuilder is not valid. Before valid json comes, I have some not valid string like \_id, \_type etc. It seems the meatinfo is somehow prefixed to the real json. How can I solve this issue?
> 
> Here is an example of the returned json:
> 
> "\_index""content","\_type" "content","\_id" "[Page not found | PageGroup](http://www.page.com/a/1.html%22,%22_version)"  
> 1,"found" true,"term\_vectors"  
> {"main":{"field\_statistics":{"sum\_doc\_freq":4,"doc\_count":1,"sum\_ttf":4},"terms":{"döner":{"doc\_freq":1,"ttf":1,"term\_freq":1,"tokens":[{"position":3,"start\_offset":16,"end\_offset":21,"payload":"PEFMUEhBTlVNPg=="}]},"einen":{"doc\_freq":1,"ttf":1,"term\_freq":1,"tokens":[{"position":2,"start\_offset":10,"end\_offset":15,"payload":"PEFMUEhBTlVNPg=="}]},"ist":{"doc\_freq":1,"ttf":1,"term\_freq":1,"tokens":[{"position":1,"start\_offset":6,"end\_offset":9,"payload":"PEFMUEhBTlVNPg=="}]},"klaus":{"doc\_freq":1,"ttf":1,"term\_freq":1,"tokens":[{"position":0,"start\_offset":0,"end\_offset":5,"payload":"PEFMUEhBTlVNPg=="}]}}}
> 
> And here is my code:
> 
> final TermVectorResponse termVectorResponse = getClient().termVector(new TermVectorRequest()
> 
> ```
> .index(this.index)
> 
> .type(this.type)
> 
> .id(id)
> 
> .termStatistics(true)
> 
> .selectedFields(new String[]{"main", "abs", "title"}))
> 
> .actionGet();
> 
> ```
> 
> XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
> 
> termVectorResponse.toXContent(builder, null);
> 
> Cheers,
> 
> Klaus

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/6ea5ce4d-1f30-434b-8154-9f401ec00515%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6ea5ce4d-1f30-434b-8154-9f401ec00515%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 12:30am UTC](https://discuss.elastic.co/t/invalid-json-from-xcontentbuilder/22329/3 "2017-07-06T00:30:49Z")

</div>


