Dealing with floating point values and JSON

Tags:

#1 nimaimalle

Since there's no support for floating point data types in nxlog, given a log entry that contains numbers with decimal points, is the best option to convert them to fixed point integers? For example, given a field $value = "123.45" (a string) extracted from a log line using regex or xm_kvp, if I go directly to_json, I end up with a string in JSON.

I don't see any way to put the value, without quotes, into JSON. Am I correct?

One workaround is to convert the value to a fixed point integer, choosing a specific precision. For example, if I were to choose to always store my values * 100, I could do the following:

if $value =~ /^(-)?([0-9]+)\.?([0-9]+)?$/
{
  if not defined($3) or size($3) == 0 $value = integer($2) * 100;
  else if size($3) == 1 $value = integer($2) * 100 + integer($3) * 10;
  else $value = integer($2) * 100 + integer(substr($3,0,2));
  if $1 == '-' $value = $value * -1;
}

Given $value = "123.45", "123", "123.4567", "123.4", or "123.", this code will assign the correct, 2-place integer value.

Is this the best current approach to converting a string representation of a floating point value to something that will result in a non-quoted value in JSON?

Thank you!

#2 Zhengshi Nxlog ✓
#1 nimaimalle
Since there's no support for floating point data types in nxlog, given a log entry that contains numbers with decimal points, is the best option to convert them to fixed point integers? For example, given a field $value = "123.45" (a string) extracted from a log line using regex or xm_kvp, if I go directly to_json, I end up with a string in JSON. I don't see any way to put the value, without quotes, into JSON. Am I correct? One workaround is to convert the value to a fixed point integer, choosing a specific precision. For example, if I were to choose to always store my values * 100, I could do the following: if $value =~ /^(-)?([0-9]+)\.?([0-9]+)?$/ { if not defined($3) or size($3) == 0 $value = integer($2) * 100; else if size($3) == 1 $value = integer($2) * 100 + integer($3) * 10; else $value = integer($2) * 100 + integer(substr($3,0,2)); if $1 == '-' $value = $value * -1; } Given $value = "123.45", "123", "123.4567", "123.4", or "123.", this code will assign the correct, 2-place integer value. Is this the best current approach to converting a string representation of a floating point value to something that will result in a non-quoted value in JSON? Thank you!

This is probably close to the best way you could run it until we have support for floating point data types native.

You could extend this a bit though and use xm_perl to do the KVP->JSON conversion as well as manipulating your fields as Perl supports floating point.