# How to handle key value pairs when keys and values are in different fields

**URL:** https://discuss.elastic.co/t/how-to-handle-key-value-pairs-when-keys-and-values-are-in-different-fields/360460
**Category:** Logstash
**Created:** [May 29, 2024, 12:01pm UTC](https://discuss.elastic.co/t/how-to-handle-key-value-pairs-when-keys-and-values-are-in-different-fields/360460 "2024-05-29T12:01:05Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![bvoros](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bvoros/32/5246_2.png) [@bvoros](https://discuss.elastic.co/u/bvoros)
#### Post date: [May 29, 2024, 12:01pm UTC](https://discuss.elastic.co/t/how-to-handle-key-value-pairs-when-keys-and-values-are-in-different-fields/360460/1 "2024-05-29T12:01:05Z")

</div>

Hello,

In the events that are being processed there are key value pairs where the keys and the values are in two fields. What is the best way to handle these?

Example data:

```auto
data.httpRequest.headers.name
[host, connection, accept, accept-language, sec-fetch-mode, user-agent, accept-encoding]

data.httpRequest.headers.value
[api.website.com, keep-alive, */*, *, cors, undici, br, gzip, deflate]

```

Thanks all once again,

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [May 29, 2024, 12:50pm UTC](https://discuss.elastic.co/t/how-to-handle-key-value-pairs-when-keys-and-values-are-in-different-fields/360460/2 "2024-05-29T12:50:37Z")

</div>

You could start with something like

```
input { generator { count => 1 lines => [''] } }

output { stdout { codec => rubydebug { metadata => true } } }
filter {
    mutate { remove_field => ["event", "host", "log"] }

    mutate {
        add_field => {
            "[data][httpRequest][headers][name]" => ["host", "connection", "accept", "accept-language", "sec-fetch-mode", "user-agent", "accept-encoding"]
            "[data][httpRequest][headers][value]" => ["api.website.com", "keep-alive", "*/*", "*", "cors", "undici", "br", "gzip", "deflate"]
        }
    }
    ruby {
        code => '
            begin
                names = event.get("[data][httpRequest][headers][name]")
                values = event.get("[data][httpRequest][headers][value]")

                names.each_index { |i|
                    event.set(names[i], values[i])
                }
            rescue
            end
        '
    }
}

```

You have seven names and nine values, so that code ignores the last two. That may not be what you want to do.

---

<div class="post-metadata">

### Author: ![bvoros](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bvoros/32/5246_2.png) [@bvoros](https://discuss.elastic.co/u/bvoros)
#### Post date: [May 29, 2024, 1:05pm UTC](https://discuss.elastic.co/t/how-to-handle-key-value-pairs-when-keys-and-values-are-in-different-fields/360460/3 "2024-05-29T13:05:52Z")

</div>

Thank you, will try this.

---

<div class="post-metadata">

### Author: ![bvoros](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bvoros/32/5246_2.png) [@bvoros](https://discuss.elastic.co/u/bvoros)
#### Post date: [May 29, 2024, 2:09pm UTC](https://discuss.elastic.co/t/how-to-handle-key-value-pairs-when-keys-and-values-are-in-different-fields/360460/4 "2024-05-29T14:09:44Z")

</div>

Hello again, if the list is dynamic, would the following work.  
Is it possible to iterate through the list of names and add the fields like that?  
Thanks again,

```auto
    ruby {
        code => '
            begin
                names = event.get("[data][httpRequest][headers][name]")
                values = event.get("[data][httpRequest][headers][value]")

                names.each_index { |i|
                    event.add_field("[data][httpRequest][headers]names[i]")
                    event.set("[data][httpRequest][headers]names[i]", values[i])
                }
            rescue
            end
        '
    }

```

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [May 29, 2024, 3:19pm UTC](https://discuss.elastic.co/t/how-to-handle-key-value-pairs-when-keys-and-values-are-in-different-fields/360460/5 "2024-05-29T15:19:35Z")

</div>

> [@bvoros](#):
>
> ```auto
> event.add_field("[data][httpRequest][headers]names[i]")
> event.set("[data][httpRequest][headers]names[i]", values[i])
> 
> ```

There is no .add\_field method in the event API. And for the .set you will need to use string magic to do the interpolation

```
event.set("[data][httpRequest][headers][#{names[i]}]", values[i])

```
