# Logstash mutate - convert empty string to null

**URL:** https://discuss.elastic.co/t/logstash-mutate-convert-empty-string-to-null/91263
**Category:** Logstash
**Created:** [June 29, 2017, 10:11am UTC](https://discuss.elastic.co/t/logstash-mutate-convert-empty-string-to-null/91263 "2017-06-29T10:11:22Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![selivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/selivan/32/18914_2.png) [@selivan](https://discuss.elastic.co/u/selivan)
#### Post date: [June 29, 2017, 10:11am UTC](https://discuss.elastic.co/t/logstash-mutate-convert-empty-string-to-null/91263/1 "2017-06-29T10:11:22Z")

</div>

There is a field in log file, which contains IP address or empty string if IP address is not available.  
Elasticsearch index has mapping, that maps "ip" type to this field. When field value is empty string, logstash can not save data to index:

`[WARN][logstash.outputs.elasticsearch] Could not index event to Elasticsearch`  
`...`  
`"error"=>{"type"=>"mapper_parsing_exception", "reason"=>"failed to parse [doc.ip]", "caused_by"=>{"type"=>"illegal_argument_exception", "reason"=>"'' is not an IP string literal."}}}}}`

To fix this, I need to convert empty string to null with logstash. This doesn't work:

```
if [doc][ip] == "" {
  mutate {
    replace => { "[doc][ip]" => null }
  }
}

```

Because it converts value to string "null". "nil" doesn't work either.

What is correct syntax to convert field to null value?

---

<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 29, 2017, 10:20am UTC](https://discuss.elastic.co/t/logstash-mutate-convert-empty-string-to-null/91263/2 "2017-06-29T10:20:21Z")

</div>

The Logstash configuration language doesn't support null values. You'll have to use a ruby filter. Or could you just remove the field?

---

<div class="post-metadata">

### Author: ![paz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paz/32/28003_2.png) [@paz](https://discuss.elastic.co/u/paz)
#### Post date: [June 29, 2017, 10:25am UTC](https://discuss.elastic.co/t/logstash-mutate-convert-empty-string-to-null/91263/3 "2017-06-29T10:25:19Z")

</div>

Why not just delete the field? It's more straightforward than trying to force null values from the logstash side.

```auto
if [doc][ip] == "" {
  mutate {
   remove_field => ["[doc][ip]"]
  }
}
```

You could also take a look on [this mapping setting](https://www.elastic.co/guide/en/elasticsearch/reference/current/null-value.html)

---

<div class="post-metadata">

### Author: ![selivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/selivan/32/18914_2.png) [@selivan](https://discuss.elastic.co/u/selivan)
#### Post date: [June 29, 2017, 11:04am UTC](https://discuss.elastic.co/t/logstash-mutate-convert-empty-string-to-null/91263/4 "2017-06-29T11:04:27Z")

</div>

Thank you, @paz and @magnusbaeck. Removing the field instead of setting it to null solves the problem 👍

---

<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 27, 2017, 11:04am UTC](https://discuss.elastic.co/t/logstash-mutate-convert-empty-string-to-null/91263/5 "2017-07-27T11:04:35Z")

</div>

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