Make Json flattening more dynamic to process nested json arrays/objects

Hi, I'm trying to flatten a complex json file, that contains a nested - multilevel - arrays/objects, something like this:

{
    "body":{
        "reg_dicts":[
            {
                "sectionsA":[
                    {
                        "val":"1"
                    },
                    {
                        "val":"2"
                    },
                    {
                        "val":"3"
                    }
                ]
            },
            {
                "sectionsB":[
                    {
                        "val":"4"
                    },
                    {
                        "val":"5"
                    },
                    {
                        "val":"6"
                    }
                ]
            }
        ]
    }
}

I used what's mentioned in this topic Indent any json file nested fields and make it flat, but Unfortunately it only works for a one child/root arrays, in this case I need to make more dynamic .. any help is appreciated.

Here is the ruby code, in the topic I mentioned above, I used to flatten but it didn't work:

def register(params)
    @field = params['field']
end

def flatten(object, name, event)
    if object
        if object.kind_of?(Hash) and object != {}
            object.each { |k, v| flatten(v, "#{name}.#{k}", event) }
        else
            event.set(name, object)
        end
    end
end

def filter(event)
    o = event.get(@field)
    if o
        flatten(o, @field, event)
    end
    event.remove(@field)
    [event]
end

Calling it in the config file:

ruby {
        path => "/home/user/flattenJSON.rb"
        script_params => { "field" => "body" }
    }

I'm getting this output, which it's not what I'm looking for:

{
    "body_reg_dicts":[
        {
            "sectionsA":[
                {
                    "val":"1"
                },
                {
                    "val":"2"
                },
                {
                    "val":"3"
                }
            ]
        },
        {
            "sectionsB":[
                {
                    "val":"4"
                },
                {
                    "val":"5"
                },
                {
                    "val":"6"
                }
            ]
        }
    ],
    "@timestamp":"2019-09-23T21:19:35.663Z",
    "headers":{
        "destination_channel":"basel.test"
    },
    "@version":"1",
    "type":"pipeline.inference.done"
}

What are you looking for? It's not obvious to me how to flatten an array.

@Badger
As I mentioned before, the ruby code, attached up :point_up_2:, flattens a json that contains one field that is an array of strings. But, what I need to do is to flatten a json that contains a multi-level/nested arrays.

{
    "body":{
        "reg_dicts":[
            {
                "sectionsA":[
                    {
                        "val":"1"
                    },
                    {
                        "val":"2"
                    },
                    {
                        "val":"3"
                    }
                ]
            },
            {
                "sectionsB":[
                    {
                        "val":"4"
                    },
                    {
                        "val":"5"
                    },
                    {
                        "val":"6"
                    }
                ]
            }
        ]
    }
}

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