# How to convert data types?

**URL:** <https://discuss.elastic.co/t/how-to-convert-data-types/180250>\
**Category:** Logstash\
**Created:** [May 8, 2019, 8:45pm UTC](https://discuss.elastic.co/t/how-to-convert-data-types/180250 "2019-05-08T20:45:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![orx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/orx/32/45846_2.png) [@orx](https://discuss.elastic.co/u/orx)\
**Post date:** [May 8, 2019, 8:45pm UTC](https://discuss.elastic.co/t/how-to-convert-data-types/180250/1 "2019-05-08T20:45:57Z")

</div>

Hi,  
I want to parse the following data which is in the .txt file (separated by tabs):  
1 apple tree-a city1  
2 banana tree-b city2

```auto
input {
  file {
    path => "path-to-file"
    start_position => "beginning"
  }
}

filter {
  grok {
    match => { "message" => "%{NUMBER:id}\t%{WORD:fruit} etc... }    
  }
}

```

My input is a txt file and the filter works. My question is, can I use the following (and how):

```auto
filter {
  mutate {
    convert => ["id","integer"]
    convert => ["fruit","string"]
    convert => ["tree","string"]
    convert => ["city","string"]
}
}

```

Because what I get in Kibana is the entire unparsed message with all the fields inside. Logstash says config is fine and runs OK. But something is wrong during mapping data types, as I understand it. Not sure how it should be done otherwise. Thanks for any advise or idea. I'm using ELK 7.

---

<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 9, 2019, 12:00am UTC](https://discuss.elastic.co/t/how-to-convert-data-types/180250/2 "2019-05-09T00:00:59Z")

</div>

> [@orx](#):
>
> My input is a txt file and the filter works.

You are saying that the grok filter parses [message] into those fields? Because I would not expect that unless you have set [config.support\_escapes](https://www.elastic.co/guide/en/logstash/current/logstash-settings-file.html). \t is not parsed as a tab in grok, use a literal tab in the grok pattern (obviously if you use an editor like vi that means you cannot have the expandtab option enabled)

```
filter {
    mutate {
        convert => ["id","integer"]
        convert => ["fruit","string"]
        convert => ["tree","string"]
        convert => ["city","string"]
    }
}

```

Not sure if this works or not (logstash can be surprisingly forgiving of using arrays where hashes are expected, but surprisingly unforgiving where duplicate options occur). I would write this as

```
mutate {
    convert => {
        "id" =>"integer"
        "fruit" => "string"
        "tree" => "string"
        "city" => "string"
    }
}

```

That said, grok will produce a string by default for any pattern match, and you can adjust that by changing %{NUMBER:id} to %{NUMBER:id:int} and then remove the mutate filter.

---

<div class="post-metadata">

**Author:** ![orx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/orx/32/45846_2.png) [@orx](https://discuss.elastic.co/u/orx)\
**Post date:** [May 9, 2019, 1:36pm UTC](https://discuss.elastic.co/t/how-to-convert-data-types/180250/3 "2019-05-09T13:36:55Z")

</div>

Thanks for your reply Badger, unfortunately I didn't understand how tabulation works in grok.  
Simple example:

```auto
1 apple sweet-01
2 lime bitter-02

```

separated by space

```auto
filter {
  grok {
    match => { "message" => "%{NUMBER:id} %{WORD:fruit} (?<taste>[\w+-]+)" }
  }
}

```

This pattern passes in Kibana grok debugger.  
This time my question is, if I add a tab instead of space in my simple data, how grok filter changes, to reflect tabulation ? thanks

---

<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 9, 2019, 1:40pm UTC](https://discuss.elastic.co/t/how-to-convert-data-types/180250/4 "2019-05-09T13:40:16Z")

</div>

If you have a tab in your data, you need a tab in your grok pattern.

If you have a space in your data, you need a space in your grok pattern.

Or you could use \s, or perhaps \s+ to match one or more of either.

---

<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:** [June 6, 2019, 1:40pm UTC](https://discuss.elastic.co/t/how-to-convert-data-types/180250/5 "2019-06-06T13:40:17Z")

</div>

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