# How to convert a json field to string

**URL:** <https://discuss.elastic.co/t/how-to-convert-a-json-field-to-string/49898>\
**Category:** Logstash\
**Created:** [May 12, 2016, 12:24pm UTC](https://discuss.elastic.co/t/how-to-convert-a-json-field-to-string/49898 "2016-05-12T12:24:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![changxy](https://avatars.discourse-cdn.com/v4/letter/c/2acd7d/32.png) [@changxy](https://discuss.elastic.co/u/changxy)\
**Post date:** [May 12, 2016, 12:24pm UTC](https://discuss.elastic.co/t/how-to-convert-a-json-field-to-string/49898/1 "2016-05-12T12:24:15Z")

</div>

my log (json) content is：  
{"release\_time": "1998-01-01", "home\_url": "[http://www.777.com/show\_page/id\_001.html](http://www.777.com/show_page/id_001.html)", "isVip": "", "channel": "777"}  
{"release\_time": "2009", "home\_url": "[http://www.666.com/show\_page/id\_002.html](http://www.666.com/show_page/id_002.html)", "isVip": true, "channel": "666"}  
{"release\_time": "", "home\_url": "[http://www.666.com/show\_page/id\_003.html](http://www.666.com/show_page/id_003.html)", "isVip": "", "channel": "666"}

logstash config :  
input{}  
filter{  
json{  
source =\> "message"  
}  
mutate {  
convert =\> { "[release\_time]" =\> "string" }  
convert =\> { "[isVip]" =\> "string" }  
}  
}  
output {  
elasticsearch {index =\> "test-%{+YYYY.MM.dd}"}  
}

then logstash error logs is:  
`"reason"=>"failed to parse [release_time]", "caused_by"=>{"type"=>"illegal_argument_exception", "reason"=>"Invalid format: \"\""`

curl -XGET [http://localhost:9200/test-2016.05.12](http://localhost:9200/test-2016.05.12)  
...  
"release\_time":{"type":"date","format":"strict\_date\_optional\_time||epoch\_millis"}

---

<div class="post-metadata">

**Author:** ![fbaligand](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fbaligand/32/5657_2.png) [@fbaligand](https://discuss.elastic.co/u/fbaligand)\
**Post date:** [May 12, 2016, 2:25pm UTC](https://discuss.elastic.co/t/how-to-convert-a-json-field-to-string/49898/2 "2016-05-12T14:25:26Z")

</div>

Hi, the right configuration is :

```
mutate {
                convert => { "release_time" => "string" }
                convert => { "isVip" => "string" }
}

```

But given your initial log, you don't need conversion.  
In logstash, it is a string.

Your problem is in elasticsearch : when mapping is not defined, by default, elasticsearch tries to automatically define the type. If elasticsearch detects a date format, then it converts the field to date and mapping is date.

So if you really want string into elasticsearch, you have to explicitly define elasticsearch mapping for this field.

I advice you to define a elasticsearch template in elasticsearch logstash output :  
[https://www.elastic.co/guide/en/logstash/2.3/plugins-outputs-elasticsearch.html#plugins-outputs-elasticsearch-template](https://www.elastic.co/guide/en/logstash/2.3/plugins-outputs-elasticsearch.html#plugins-outputs-elasticsearch-template)

---

<div class="post-metadata">

**Author:** ![changxy](https://avatars.discourse-cdn.com/v4/letter/c/2acd7d/32.png) [@changxy](https://discuss.elastic.co/u/changxy)\
**Post date:** [May 23, 2016, 3:12am UTC](https://discuss.elastic.co/t/how-to-convert-a-json-field-to-string/49898/3 "2016-05-23T03:12:37Z")

</div>

thanks

---

<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 6, 2017, 4:56am UTC](https://discuss.elastic.co/t/how-to-convert-a-json-field-to-string/49898/4 "2017-07-06T04:56:43Z")

</div>


