# Json direct to Java class?

**URL:** https://discuss.elastic.co/t/json-direct-to-java-class/6585
**Category:** Elasticsearch
**Created:** [February 3, 2012, 4:40pm UTC](https://discuss.elastic.co/t/json-direct-to-java-class/6585 "2012-02-03T16:40:12Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Ben\_Rometsch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ben_rometsch/32/3003_2.png) [@Ben\_Rometsch](https://discuss.elastic.co/u/Ben_Rometsch)
#### Post date: [February 3, 2012, 4:40pm UTC](https://discuss.elastic.co/t/json-direct-to-java-class/6585/1 "2012-02-03T16:40:12Z")

</div>

Hi,

I've been using XContentBuilder within the Java API to index  
documents. Is there a corresponding way to populate objects with JSON  
data? E.g. in Gson you can say

BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);

Thanks

---

<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 3, 2012, 4:50pm UTC](https://discuss.elastic.co/t/json-direct-to-java-class/6585/2 "2012-02-03T16:50:12Z")

</div>

So you can use gson, or Jackson which is embedded in ES or your own version of Jackson.

XContentBuilder is nice when you don't need to serialize/deserialize beans.

HTH  
David.

Le 3 févr. 2012 à 17:40, Ben Rometsch [ben@solidstategroup.com](mailto:ben@solidstategroup.com) a écrit :

> Hi,
> 
> I've been using XContentBuilder within the Java API to index  
> documents. Is there a corresponding way to populate objects with JSON  
> data? E.g. in Gson you can say
> 
> BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);
> 
> Thanks

---

<div class="post-metadata">

### Author: ![louis\_gueye](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/louis_gueye/32/2772_2.png) [@louis\_gueye](https://discuss.elastic.co/u/louis_gueye)
#### Post date: [February 3, 2012, 4:54pm UTC](https://discuss.elastic.co/t/json-direct-to-java-class/6585/3 "2012-02-03T16:54:13Z")

</div>

You can use Jackson to serialize java to json and vice-versa.

The json will have to be converted to byte array for better performance.

Feel free to read

> **[Add search features to your application, try Elasticsearch part 1 : the concepts](https://deepintojee.wordpress.com/2012/01/20/add-search-features-to-your-application-try-elasticsearch-part-1-the-concepts/)**
>
> Hi all, The ultimate goal of a search engine is to provide fast, reliable, easy to use, scalable search features to a software. But before diving into complex and technical considerations we should…

You can browse the code source (focus on SearchEngineImpl) for a working  
example.

Cheers,

Louis.

2012/2/3 Ben Rometsch [ben@solidstategroup.com](mailto:ben@solidstategroup.com)

> Hi,
> 
> I've been using XContentBuilder within the Java API to index  
> documents. Is there a corresponding way to populate objects with JSON  
> data? E.g. in Gson you can say
> 
> BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);
> 
> Thanks

--  
Cordialement/Regards,

Louis GUEYE  
linkedin [http://fr.linkedin.com/in/louisgueye](http://fr.linkedin.com/in/louisgueye) |  
blog[http://deepintojee.wordpress.com/](http://deepintojee.wordpress.com/)|  
twitter [http://twitter.com/#!/lgueye](http://twitter.com/#%21/lgueye)

---

<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:40am UTC](https://discuss.elastic.co/t/json-direct-to-java-class/6585/4 "2017-07-06T03:40:42Z")

</div>


