# Insert nested JSON with XContentBuilder

**URL:** <https://discuss.elastic.co/t/insert-nested-json-with-xcontentbuilder/80297>\
**Category:** Elasticsearch\
**Created:** [March 28, 2017, 12:44pm UTC](https://discuss.elastic.co/t/insert-nested-json-with-xcontentbuilder/80297 "2017-03-28T12:44:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![samdev](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@samdev](https://discuss.elastic.co/u/samdev)\
**Post date:** [March 28, 2017, 12:44pm UTC](https://discuss.elastic.co/t/insert-nested-json-with-xcontentbuilder/80297/1 "2017-03-28T12:44:36Z")

</div>

I have data flowing into a document from two sources. One is text which needs to be converted to JSON. Simple to add it using:

```
jsonBuilder.field(name, value);

```

The other source has data that is already formatted as JSON.

`String nestedExample = "{\"Details\": [{\"Login\":\"ABC\", \"DateTime\": 123213113},{\"Login\":\"BCD\", \"DateTime\": 123213213}, {\"Login\":\"EFG\", \"DateTime\": 123213413}]}";`

I found that I could use jsonBuilder,rawField to insert prebuilt JSON, however it always wants a field name.If I use:

```
InputStream jsonStream = new ByteArrayInputStream(nestedExample.getBytes());
jsonBuilder.rawField(name, jsonStream);

```

It creates an additional field name which creates an additional layer of nesting to my data structure.

I cannot find a way to insert this raw data that doesnt require me to specify a field name, which creates an additional layer of nesting that I do not want.

I have also tried using XContentParser and then copyCurrentStructure, but that also needs a field name and again creates an additioanl layer of nesting.

I'm sure this is really simple, but how can I insert raw JSON into a document field using XContentBuilder?

Thanks!

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [March 28, 2017, 1:43pm UTC](https://discuss.elastic.co/t/insert-nested-json-with-xcontentbuilder/80297/2 "2017-03-28T13:43:34Z")

</div>

I think using `copyCurrentStructure(XContentParser)` is the way to go here. Here's some example code:

```auto
XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint();
        String nestedExample = "{\"foo\": {\"bar\":\"baz\"}}";
        XContentParser parser = JsonXContent.jsonXContent
                .createParser(NamedXContentRegistry.EMPTY, nestedExample);
        builder.startObject();
        builder.startArray("name");
        builder.copyCurrentStructure(parser);
        builder.endArray();
        builder.endObject();
        System.out.println(builder.bytes().utf8ToString());

```

prints

```auto
{
  "name" : [
    {
      "foo" : {
        "bar" : "baz"
      }
    }
  ]
}

```

and if you need the nested object as a field value you can do:

```auto
...
builder.field("name");
builder.copyCurrentStructure(parser);
...

```

---

<div class="post-metadata">

**Author:** ![samdev](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@samdev](https://discuss.elastic.co/u/samdev)\
**Post date:** [March 28, 2017, 1:58pm UTC](https://discuss.elastic.co/t/insert-nested-json-with-xcontentbuilder/80297/3 "2017-03-28T13:58:41Z")

</div>

Thanks for the response!

It is the field called "name" in your example that I am trying to avoid. Is there a way that I can use the field "foo" in your example as the field name (basically telling XcontentBuilder, here is a JSON object to insert and you should take the existing field name from that) without having to specify another one.

Also, XcontentParser only seems to return me the first object in an array. How can I get to the other values, when I supply an array of objects?

Thanks!

---

<div class="post-metadata">

**Author:** ![samdev](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@samdev](https://discuss.elastic.co/u/samdev)\
**Post date:** [March 28, 2017, 2:10pm UTC](https://discuss.elastic.co/t/insert-nested-json-with-xcontentbuilder/80297/4 "2017-03-28T14:10:12Z")

</div>

I got this working now! Thank you. I hadn't realised that it was possible to just call jsonBuilder.field(name) and then to do the copyCurrrentStructure(exampleString) immediately after. This solved it for me!

Thank you!

---

<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:** [April 25, 2017, 2:10pm UTC](https://discuss.elastic.co/t/insert-nested-json-with-xcontentbuilder/80297/5 "2017-04-25T14:10:13Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
