# The best practice to nest a hash?

**URL:** <https://discuss.elastic.co/t/the-best-practice-to-nest-a-hash/91373>\
**Category:** Logstash\
**Created:** [June 30, 2017, 5:56am UTC](https://discuss.elastic.co/t/the-best-practice-to-nest-a-hash/91373 "2017-06-30T05:56:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucianspec](https://avatars.discourse-cdn.com/v4/letter/l/3bc359/32.png) [@lucianspec](https://discuss.elastic.co/u/lucianspec)\
**Post date:** [June 30, 2017, 5:56am UTC](https://discuss.elastic.co/t/the-best-practice-to-nest-a-hash/91373/1 "2017-06-30T05:56:28Z")

</div>

my event data like this...

> ```
> "thread" => "Thread-74-__system-executor[-1 -1]",
> "message" => "2017-06-19 16:46:12.750 o.a.s.d.executor Thread-74-__system-executor[-1 -1] [INFO] Prepared bolt__system:(-1)",
> "type" => "o.a.s.d.executor",
> "content" => "Prepared bolt __system:(-1)",
> "timestamp" => "2017-06-19 16:46:12.750"
> 
> ```

i want to put [timestamp, content, type, thread] replace the old message key & change the value type to a Hash

we found the best way is

> ```
> mutate {
> add_field => {
> "[data][timestamp]" => "%{timestamp}"
> "[data][thread]" => "%{thread}"
> }
> remove_field => "message"
> # rename => {"data" => "message"} #this will fail!!!
> }
> 
> ```
> 
> mutate { rename =\> {"data" =\> "message"} }

this is so dirty....

data =\> {"thread" =\> "%{thread}", "timestamp" =\> "%{timestamp}"} will produce a array not a Hash...

so what's the best practice??? should we need to use ruby to convert a array to a Hash???

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [June 30, 2017, 8:03am UTC](https://discuss.elastic.co/t/the-best-practice-to-nest-a-hash/91373/2 "2017-06-30T08:03:33Z")

</div>

How about removing the existing `message` field and renaming the `thread`, `timestamp`, and `content` fields?

---

<div class="post-metadata">

**Author:** ![lucianspec](https://avatars.discourse-cdn.com/v4/letter/l/3bc359/32.png) [@lucianspec](https://discuss.elastic.co/u/lucianspec)\
**Post date:** [June 30, 2017, 8:15am UTC](https://discuss.elastic.co/t/the-best-practice-to-nest-a-hash/91373/3 "2017-06-30T08:15:46Z")

</div>

em....awesme idea...let me try  
besides, Can you explain why rename a newly added field inside the same block will fail?? i am confused about it.

---

<div class="post-metadata">

**Author:** ![lucianspec](https://avatars.discourse-cdn.com/v4/letter/l/3bc359/32.png) [@lucianspec](https://discuss.elastic.co/u/lucianspec)\
**Post date:** [June 30, 2017, 8:43am UTC](https://discuss.elastic.co/t/the-best-practice-to-nest-a-hash/91373/4 "2017-06-30T08:43:47Z")

</div>

i am sorry it won't work..

> ```
> mutate { 
> remove_field => "message"
> rename => {
> "timestamp" => "[message][timestamp]"
> }
> }
> 
> ```

"exception"=\>"expecting List or Map, found class org.logstash.bivalues.StringBiValue",

but this will work

> ```
> mutate { 
> remove_field => "message"
> }
> mutate {
> rename => {
> "timestamp" => "[message][timestamp]"
> "thread" => "[message][thread]"
> }
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [June 30, 2017, 9:20am UTC](https://discuss.elastic.co/t/the-best-practice-to-nest-a-hash/91373/5 "2017-06-30T09:20:01Z")

</div>

That's expected. Options in a mutate plugin aren't evaluated in the order specified but in this order:

> <https://github.com/logstash-plugins/logstash-filter-mutate/blob/v3.1.5/lib/logstash/filters/mutate.rb#L213-L229>

---

<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 28, 2017, 9:20am UTC](https://discuss.elastic.co/t/the-best-practice-to-nest-a-hash/91373/6 "2017-07-28T09:20:09Z")

</div>

Closed on July 28, 2017, 9:20am UTC

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