1
answer

From this example, in the docs: Name=Mike, Weight=64, Age=24, Pet=dog, Height=172

The sample shows accessing the fields like this, effectively casting certain values to integers: if ( integer($Weight) > integer($Height) - 100 )

However, I am using parse_kvp on data that I don't necessarily know the format of, then converting the data into JSON. In JSON, all of the values are quoted, including numeric ones. I need the numeric fields to be unquoted in JSON.

Is there a way to iterate through all fields in an Exec statement? I could test for numeric values and reassign them, casting to integer or float (but I don't think there's a float type)

I also thought about transforming the JSON string with s/"([0-9.]+)"/$1/g but this sort of regex is not yet supported in nxlog.

Any suggestions how to take Name=Mike, Weight=64, Age=24, Pet=dog, Height=172.5 and get the following JSON without referencing the fields by name? {"Name":"Mike","Weight":64,"Age":24,"Pet":"dog","Height":172.5}

(no quotes around numbers)

AskedMarch 8, 2019 - 2:37pm

Comments (1)

  • nimaimalle's picture

    Maybe something like the QuoteMethod directive could work?

    Feels like QuoteMethod needs a "NonNumeric" option which will only quote values that aren't valid numeric expressions:

    Regexp to test if a string is a valid numeric value, including optional decimal points and exponent.

    ^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$

    March 8, 2019 - 2:56pm

Answer (1)

Being able to detect numeric values in parse_kvp() sounds like a reasonable enhancement request.
Iteration over fields is not supported directly but you can do this via xm_perl. See the example in the user guide.

AnsweredMarch 8, 2019 - 3:36pm