# How to use scripted field for converting string to int based on condition?

**URL:** <https://discuss.elastic.co/t/how-to-use-scripted-field-for-converting-string-to-int-based-on-condition/144203>\
**Category:** Kibana\
**Created:** [August 13, 2018, 3:44pm UTC](https://discuss.elastic.co/t/how-to-use-scripted-field-for-converting-string-to-int-based-on-condition/144203 "2018-08-13T15:44:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Vasquez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_vasquez/32/22088_2.png) [@Matt\_Vasquez](https://discuss.elastic.co/u/Matt_Vasquez)\
**Post date:** [August 13, 2018, 3:44pm UTC](https://discuss.elastic.co/t/how-to-use-scripted-field-for-converting-string-to-int-based-on-condition/144203/1 "2018-08-13T15:44:21Z")

</div>

Can someone help me out? I am trying to convert a field I have from a string to an int when a condition is met.

This is what I have so far:

if (doc['burn.keyword'].value == "Yes") {  
return 1  
}elseif (doc['burn.keyword'].value == "No") {  
return -1  
}

---

<div class="post-metadata">

**Author:** ![cjcenizal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cjcenizal/32/11216_2.png) [@cjcenizal](https://discuss.elastic.co/u/cjcenizal)\
**Post date:** [August 13, 2018, 11:36pm UTC](https://discuss.elastic.co/t/how-to-use-scripted-field-for-converting-string-to-int-based-on-condition/144203/2 "2018-08-13T23:36:34Z")

</div>

Hi Matt,

I'm able to create a script like this:

```auto
if (doc['geo.src'].value == "US") {
  return 1
}

if (doc['geo.src'].value == "CN") {
  return -1
}

return 0

```

The script is of type "number" and a format of "default".

It looks like it returns the correct values. Can you give this a shot and if it doesn't work, can you tell me what error you're seeing?

Thanks,  
CJ

---

<div class="post-metadata">

**Author:** ![Matt\_Vasquez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_vasquez/32/22088_2.png) [@Matt\_Vasquez](https://discuss.elastic.co/u/Matt_Vasquez)\
**Post date:** [August 14, 2018, 3:24pm UTC](https://discuss.elastic.co/t/how-to-use-scripted-field-for-converting-string-to-int-based-on-condition/144203/3 "2018-08-14T15:24:26Z")

</div>

This works, 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:** [September 11, 2018, 3:24pm UTC](https://discuss.elastic.co/t/how-to-use-scripted-field-for-converting-string-to-int-based-on-condition/144203/4 "2018-09-11T15:24:34Z")

</div>

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