Home > Software engineering >  Splitting a json array format with same fields name
Splitting a json array format with same fields name

Time:11-11

Currently, I have this kind of JSON array with the same field, what I wanted is to split this data into an independent field and the field name is based on a "name" field

events.parameters (this is the field name of the JSON array)

{
"name": "USER_EMAIL",
"value": "[email protected]"
},
{
"name": "DEVICE_ID",
"value": "Wdk39Iw-akOsiwkaALw"
},
{
"name": "SERIAL_NUMBER",
"value": "9KJUIHG"
}

expected output:

events.parameters.USER_EMAIL : [email protected]
events.parameters.DEVICE_ID: Wdk39Iw-akOsiwkaALw
events.parameters.SERIAL_NUMBER : 9KJUIHG

Thanks.

CodePudding user response:

Tldr;

There is no filter that does exactly what you are looking for.

You will have to use the ruby filter

CodePudding user response:

I just fixed the problem, for everyone wondering here's my ruby script

   if [events][parameters] {
      ruby {
        code => '
          event.get("[events][parameters]").each { |a|
            name = a["name"]
            value = a["value"]
            event.set("[events][parameters_split][#{name}]", value)
          }
        '
      }
    }

the output was just like what I wanted.

Cheers!

  • Related