Convert value for nested json array


#1

Hello,
I have a json input like this one:
{
"fieldA" : "valueA",
"fieldB" : [ { "nestedstring" : "string1a", "nestedint": number1b }, { "nestedstring" : "string2a", "nestedint": number2b }, ...]
}

I would like to convert the type of all "nestedint" from number (long integer) to string ... I have made some test like:
ruby {
code => "
event.get('fieldB').each { |k|
k['nestedint'] = k['nestedint'].to_s
}
"
}

But ... without any result (and without any error) ... how can I solve my problem?

Thanks in advance


(Paris Mermigkas) #2

Well, there are a couple issues with the above:

  1. You need to use the .set method to alter a field inside a Ruby block.
  2. You need to provide a full tree path on the .set method to change that field.

A solution would be to iterate with index and use that to construct the full path, like so

    ruby {
        code => "
            event.get('fieldB').each_index { |i|
                event.set('[fieldB]['+i.to_s+'][nestedint]', event.get('[fieldB]['+i.to_s+'][nestedint]').to_s)
            }
        "
    }

#3

Thank you!


(system) #4

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