# How to validate a json value is numeric

**URL:** <https://discuss.elastic.co/t/how-to-validate-a-json-value-is-numeric/339562>\
**Category:** Logstash\
**Created:** [July 28, 2023, 2:59pm UTC](https://discuss.elastic.co/t/how-to-validate-a-json-value-is-numeric/339562 "2023-07-28T14:59:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sc5283](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@sc5283](https://discuss.elastic.co/u/sc5283)\
**Post date:** [July 28, 2023, 2:59pm UTC](https://discuss.elastic.co/t/how-to-validate-a-json-value-is-numeric/339562/1 "2023-07-28T14:59:07Z")

</div>

noob question

I have a JSON as follows:  
{"attr1":"One", "attr2":"300"}  
{"attr1":"Two","attr2":45.0}  
{"attr1":"Three","attr2":"Not Set"}

attr2 is a numeric value

How do I check if attr2 is numeric, not a string before sending to elastic ?  
I can check easily for the "Not Set"  
if [attr2] == "Not Set"  
{ drop { remove\_field =\> ["attr2"] } }

but  
ie. If attr is a string and is a numeric string then  
convert to a number value

---

<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:** [July 28, 2023, 6:57pm UTC](https://discuss.elastic.co/t/how-to-validate-a-json-value-is-numeric/339562/2 "2023-07-28T18:57:59Z")

</div>

If it is a numeric string you can use

```
 mutate { convert => { "attr2" => float } }

```

This will convert "Not set" to 0.0, so do the drop before the mutate.

---

<div class="post-metadata">

**Author:** ![sc5283](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@sc5283](https://discuss.elastic.co/u/sc5283)\
**Post date:** [July 28, 2023, 8:15pm UTC](https://discuss.elastic.co/t/how-to-validate-a-json-value-is-numeric/339562/3 "2023-07-28T20:15:33Z")

</div>

So any value that is not valid will transform to 0.0 ?  
Is there a way to check for the non-numeric value (in case another string value gets added) ?  
Rather have a NULL value than a 0.0 value

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:** [July 28, 2023, 8:45pm UTC](https://discuss.elastic.co/t/how-to-validate-a-json-value-is-numeric/339562/4 "2023-07-28T20:45:10Z")

</div>

> [@sc5283](#):
>
> Is there a way to check for the non-numeric value (in case another string value gets added) ?

You could do it in ruby

```
    ruby {
        init => '
            def is_numeric? s
                true if Float(s) rescue false
            end
        '
        code => '
            s = event.get("attr2")
            unless is_numeric? event.get("attr2") ; event.remove("attr2") ; end
        '
    }

```

---

<div class="post-metadata">

**Author:** ![sc5283](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@sc5283](https://discuss.elastic.co/u/sc5283)\
**Post date:** [July 28, 2023, 9:01pm UTC](https://discuss.elastic.co/t/how-to-validate-a-json-value-is-numeric/339562/5 "2023-07-28T21:01:27Z")

</div>

Thank you

---

<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:** [August 25, 2023, 9:02pm UTC](https://discuss.elastic.co/t/how-to-validate-a-json-value-is-numeric/339562/6 "2023-08-25T21:02:15Z")

</div>

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