# Upsert into nested field

**URL:** <https://discuss.elastic.co/t/upsert-into-nested-field/307289>\
**Category:** Logstash\
**Created:** [June 15, 2022, 2:25pm UTC](https://discuss.elastic.co/t/upsert-into-nested-field/307289 "2022-06-15T14:25:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elwdipath](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elwdipath/32/96262_2.png) [@elwdipath](https://discuss.elastic.co/u/elwdipath)\
**Post date:** [June 15, 2022, 2:25pm UTC](https://discuss.elastic.co/t/upsert-into-nested-field/307289/1 "2022-06-15T14:25:58Z")

</div>

I'm trying to upsert (push) and object into a nested field.

We have data in xml that comes in like this:

```auto
<Person>
<id>1234</id>
<name>John Smith</name>
<age/>
<position/>
</Person>
<addressInfo>
<AddressID>3424</AddressID>
<PersonID>1234</PersonID>
<address>123 xyz street</address>
<city>Town A</city>
<StateProvince>State A</StateProvince>
<Country>Country A</Country>
<PostalCode>85739</PostalCode>
</addressInfo>
<addressInfo>
<AddressID>7567</AddressID>
<PersonID>1234</PersonID>
<Address>3456 abc street</address>
<Sity>Town B</city>
<StateProvince>State B</StateProvince>
<Country>Country B</Country>
<PostalCode>12345</PostalCode>
</addressInfo>

```

Expected Output:

```auto
{
  "name": John Smith,
  "age": 34,
  "position": Accounting,
  "addresses": [
   {
        "address": "123 xyz street",
        "city": "Town A",
        "stateProvince": "State A",
        "country": "Country A",
        "postalCode": "85739"
    }, 
    {
        "address": "3456 abc street",
        "city": "Town B",
        "state": "State B",
        "country": "Country B",
        "postalCode": "12345"
    }]
}

```

Ruby Script to create addresses object:

```auto
ruby {
        code => '
          event.set("addresses", [
            {
              "address" => event.get("[profile][addressinfo][Address]"), 
              "city" => event.get("[profile][addressinfo][City]"),
              "stateProvince" => event.get("[profile][addressinfo][StateProvince]"),
              "country" => event.get("[profile][addressinfo][country]"),
              "postalCode" => event.get("[profile][addressinfo][PostalCode]")
            }
          ]
          )
        '
      }

```

Logstash Output:

```auto
elasticsearch {
        hosts => ["localhost:9200"]
        action => "update"
        doc_as_upsert => true
        document_id => "%{id}"     
        index => "sentinel"
        script => 'ctx._source.addresses.add("%{addresses}")'
    }

```

Error:

```auto
  "status"=>400, "error"=>{"type"=>"mapper_parsing_exception", "reason"=>"object mapping for [addresses] tried to parse field [null] as object, but found a concrete value"}}}}

```

---

<div class="post-metadata">

**Author:** ![elwdipath](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elwdipath/32/96262_2.png) [@elwdipath](https://discuss.elastic.co/u/elwdipath)\
**Post date:** [June 22, 2022, 8:21pm UTC](https://discuss.elastic.co/t/upsert-into-nested-field/307289/2 "2022-06-22T20:21:23Z")

</div>

bump. still trying to figure this one out

---

<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 20, 2022, 8:22pm UTC](https://discuss.elastic.co/t/upsert-into-nested-field/307289/3 "2022-07-20T20:22:11Z")

</div>

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