# Indexing Json using Java API

**URL:** https://discuss.elastic.co/t/indexing-json-using-java-api/7712
**Category:** Elasticsearch
**Created:** [May 15, 2012, 11:20pm UTC](https://discuss.elastic.co/t/indexing-json-using-java-api/7712 "2012-05-15T23:20:56Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Mohit\_Anchlia](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@Mohit\_Anchlia](https://discuss.elastic.co/u/Mohit_Anchlia)
#### Post date: [May 15, 2012, 11:20pm UTC](https://discuss.elastic.co/t/indexing-json-using-java-api/7712/1 "2012-05-15T23:20:56Z")

</div>

I am unable to find example of how to index Json String using Java API.  
Could someone help me with that?

I am converting xml to json. I see how to do it in REST api but not sure  
how this can be done in Java API

---

<div class="post-metadata">

### Author: ![Shairon\_Toledo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shairon_toledo/32/2490_2.png) [@Shairon\_Toledo](https://discuss.elastic.co/u/Shairon_Toledo)
#### Post date: [May 16, 2012, 6:17pm UTC](https://discuss.elastic.co/t/indexing-json-using-java-api/7712/2 "2012-05-16T18:17:10Z")

</div>

You can index the json as string just setting the source, looks like this

Client client = new TransportClient().addTransportAddress(new  
InetSocketTransportAddress("localhost", 9300));  
String source = yourJson.toString();

IndexResponse idxResp =  
client.prepareIndex().setIndex("documents").setType("document").setId("1")  
.setSource(source).setRefresh(true).execute().actionGet();

On Tue, May 15, 2012 at 8:20 PM, Mohit Anchlia [mohitanchlia@gmail.com](mailto:mohitanchlia@gmail.com)wrote:

> I am unable to find example of how to index Json String using Java API.  
> Could someone help me with that?
> 
> I am converting xml to json. I see how to do it in REST api but not sure  
> how this can be done in Java API

--

Shairon Toledo

> **[Microsoft Developer News | C++,C# & .NET Framework & More | CodeGuru](http://hashcode.me)**
>
> 您是否想要找关于幸运飞行艇 官方开奖历史记录 168飞艇全国统一开奖数据 还有幸运飞行艇官方开奖现场直播 幸运168飞艇全天精准稳定计划 这里都能一一提供。CodeGuru is where developers come to share ideas, articles, questions, answers, tips, tricks, comments, downloads, and more related to programming in areas including C++,...

---

<div class="post-metadata">

### Author: ![Mohit\_Anchlia](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@Mohit\_Anchlia](https://discuss.elastic.co/u/Mohit_Anchlia)
#### Post date: [May 16, 2012, 6:20pm UTC](https://discuss.elastic.co/t/indexing-json-using-java-api/7712/3 "2012-05-16T18:20:24Z")

</div>

Thanks, that helps. Can that "setType" be anything? I am assuming it's  
another level of hierarchy.

On Wed, May 16, 2012 at 11:17 AM, Shairon Toledo  
[shairon.toledo@gmail.com](mailto:shairon.toledo@gmail.com)wrote:

> You can index the json as string just setting the source, looks like this
> 
> Client client = new TransportClient().addTransportAddress(new  
> InetSocketTransportAddress("localhost", 9300));  
> String source = yourJson.toString();
> 
> IndexResponse idxResp =  
> client.prepareIndex().setIndex("documents").setType("document").setId("1")  
> .setSource(source).setRefresh(true).execute().actionGet();
> 
> On Tue, May 15, 2012 at 8:20 PM, Mohit Anchlia [mohitanchlia@gmail.com](mailto:mohitanchlia@gmail.com)wrote:
> 
> > I am unable to find example of how to index Json String using Java API.  
> > Could someone help me with that?
> > 
> > I am converting xml to json. I see how to do it in REST api but not sure  
> > how this can be done in Java API
> 
> --
> 
> Shairon Toledo  
> [http://hashcode.me](http://hashcode.me)

---

<div class="post-metadata">

### Author: ![Shairon\_Toledo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shairon_toledo/32/2490_2.png) [@Shairon\_Toledo](https://discuss.elastic.co/u/Shairon_Toledo)
#### Post date: [May 16, 2012, 6:29pm UTC](https://discuss.elastic.co/t/indexing-json-using-java-api/7712/4 "2012-05-16T18:29:30Z")

</div>

Yeap it is, you can understand that as, an index has many types.

On Wed, May 16, 2012 at 3:20 PM, Mohit Anchlia [mohitanchlia@gmail.com](mailto:mohitanchlia@gmail.com)wrote:

> Thanks, that helps. Can that "setType" be anything? I am assuming it's  
> another level of hierarchy.
> 
> On Wed, May 16, 2012 at 11:17 AM, Shairon Toledo \<[shairon.toledo@gmail.com](mailto:shairon.toledo@gmail.com)
> 
> > wrote:
> 
> > You can index the json as string just setting the source, looks like  
> > this
> > 
> > Client client = new TransportClient().addTransportAddress(new  
> > InetSocketTransportAddress("localhost", 9300));  
> > String source = yourJson.toString();
> > 
> > IndexResponse idxResp =  
> > client.prepareIndex().setIndex("documents").setType("document").setId("1")  
> > .setSource(source).setRefresh(true).execute().actionGet();
> > 
> > On Tue, May 15, 2012 at 8:20 PM, Mohit Anchlia [mohitanchlia@gmail.com](mailto:mohitanchlia@gmail.com)wrote:
> > 
> > > I am unable to find example of how to index Json String using Java API.  
> > > Could someone help me with that?
> > > 
> > > I am converting xml to json. I see how to do it in REST api but not sure  
> > > how this can be done in Java API
> > 
> > --
> > 
> > Shairon Toledo  
> > [http://hashcode.me](http://hashcode.me)

--

Shairon Toledo

> **[Microsoft Developer News | C++,C# & .NET Framework & More | CodeGuru](http://hashcode.me)**
>
> 您是否想要找关于幸运飞行艇 官方开奖历史记录 168飞艇全国统一开奖数据 还有幸运飞行艇官方开奖现场直播 幸运168飞艇全天精准稳定计划 这里都能一一提供。CodeGuru is where developers come to share ideas, articles, questions, answers, tips, tricks, comments, downloads, and more related to programming in areas including C++,...

---

<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:28am UTC](https://discuss.elastic.co/t/indexing-json-using-java-api/7712/5 "2017-07-06T03:28:32Z")

</div>


