logstash-plugins / logstash-filter-mutate

Apache License 2.0
16 stars 75 forks source link

Converting JSON Decimal to String yields scientific ("E") notation result #92

Open talvey opened 7 years ago

talvey commented 7 years ago

Given a JSON message with a decimal field value, converting that target value to a string will result in scientific "E" notation. While this isn't exactly a bug, I'd call it an unexpected result.

To recreate, pass the following simple JSON document to logstash

{
  "data" : {
    "decimal1" : 5.1
  }
}

And then use the following filter section

filter {
    json {
        source => "message"
    }
    mutate {
        convert => { "[data][decimal1]" => "string"}
    }
}

The result is "decimal1" => "0.51E1"

My guess -- the cause is the underlying java json parsing library defaulting decimal fields to Java BigDecimal, which gets translated to a Ruby BigDecimal -- calling to_s on the Ruby BigDecimal yields the "0.51E1".

bobrien27 commented 3 years ago

Anyone else having this issue? Tried multiple java 15 versions and ELK Stacks 7.12.x/7.13.0 combinations, but when using mutate { convert => { ["field"] => "string"}} I get the same results with 11.11 => "0.1111e2".

bobrien27 commented 3 years ago

mutate { add_field => { "placeholder" => "%{float_number}"} } mutate { convert => ["float_number","string"]} ruby { code => ' event.set("float_number", event.get("placeholder").slice(0..-3)) '} mutate { remove_field => [ "placeholder" ] }

My temporary workaround, if anyone has a fix or better solution any help is greatly appreciated.