# JSON document as a value using java api

**URL:** https://discuss.elastic.co/t/json-document-as-a-value-using-java-api/4132
**Category:** Elasticsearch
**Created:** [March 20, 2011, 5:34pm UTC](https://discuss.elastic.co/t/json-document-as-a-value-using-java-api/4132 "2011-03-20T17:34:17Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![vgarg](https://avatars.discourse-cdn.com/v4/letter/v/67e7ee/32.png) [@vgarg](https://discuss.elastic.co/u/vgarg)
#### Post date: [March 20, 2011, 5:34pm UTC](https://discuss.elastic.co/t/json-document-as-a-value-using-java-api/4132/1 "2011-03-20T17:34:17Z")

</div>

Hello,

I am trying to add a json document as a value in a field using the  
java ap

IndexResponse response = client  
.prepareIndex(name, type)  
.setSource(  
jsonBuilder().startObject().field("user", user)  
.field("date", new Date())  
.field("doc", json))  
.execute().actionGet();

By default elasticsearch assumes json is of type string and index the  
whole documents as a single string.  
Is there a way I can tell ES that this is a json document and specify  
the specific fields to be indexed without having to iterate through  
the entire object in code? Can I also store json as a child json  
object rather than string?

Thanks

---

<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: [March 21, 2011, 10:02am UTC](https://discuss.elastic.co/t/json-document-as-a-value-using-java-api/4132/2 "2011-03-21T10:02:21Z")

</div>

You can do that, requires a bit more coding, here is an example of how the elasticsearch flume integration does it: [https://github.com/tallpsmith/elasticflume/blob/master/src/main/java/org/elasticsearch/flume/ElasticSearchSink.java](https://github.com/tallpsmith/elasticflume/blob/master/src/main/java/org/elasticsearch/flume/ElasticSearchSink.java).  
On Sunday, March 20, 2011 at 7:34 PM, vgarg wrote:

> Hello,
> 
> I am trying to add a json document as a value in a field using the  
> java ap
> 
> IndexResponse response = client  
> .prepareIndex(name, type)  
> .setSource(  
> jsonBuilder().startObject().field("user", user)  
> .field("date", new Date())  
> .field("doc", json))  
> .execute().actionGet();
> 
> By default elasticsearch assumes json is of type string and index the  
> whole documents as a single string.  
> Is there a way I can tell ES that this is a json document and specify  
> the specific fields to be indexed without having to iterate through  
> the entire object in code? Can I also store json as a child json  
> object rather than string?
> 
> Thanks

---

<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, 4:09am UTC](https://discuss.elastic.co/t/json-document-as-a-value-using-java-api/4132/3 "2017-07-06T04:09:34Z")

</div>


