Flatten nested json array

I'm trying to flatten this json object with nested arrays into individual objects at the top level. It's a little weird because the array elements have different elements

{
    "MyData": {   
        "identifier" : "abc",
        "Query" : [
            {
                "Name" : "q1",
                "Row" : [
                    {
                        "Name" : "John",
                        "Age" : "21"
                    },
                    {
                        "Name" : "Jane",
                        "Age" : "20"
                    }
                ]
            },
            {
                "Name" : "q2",
                "Row" : [
                    {
                        "Country" : "USA",
                        "Population" : "100"
                    },
                    {
                        "Country" : "Mexico",
                        "Population" : "99"
                    } 
                ]
            }
        ]
    }
}

I'd like it to end up like this

{ 
    "PersonRow" : {
        "identifier" : "abc",
        "Type" : "q1",
        "Name" : "John",
        "Age" : "21"
    }
}
{ 
    "PersonRow" : {
        "identifier" : "abc",
        "Type" : "q1",
        "Name" : "Jane",
        "Age" : "20"
    }
}
{ 
    "CountryRow" : {
        "identifier" : "abc",
        "Type" : "q2",
        "Country" : "USA",
        "Population" : "100"
    }
}
{ 
    "CountryRow" : {
        "identifier" : "abc",
        "Type" : "q2",
        "Country" : "Mexico",
        "Population" : "99"
    }
}

Is it possible to flatten so all objects get to the top level? I'm guessing I have to do a ruby filter but not sure where to start.

Refer to this topic I created, I had a similar thing and it's solved in here

1 Like

If the data is always in that format, with persons first and countries second then

    ruby {
        code => '
            myData = event.get("MyData")
            unless myData
                return
            end

            a = []
            id = myData["identifier"]

            people = myData["Query"][0]
            type = people["Name"]
            people["Row"].each { |x|
                outer = {}
                inner = {}
                inner["indentifier"] = id
                inner["Type"] = type
                inner["Name"] = x["Name"]
                inner["Age"] = x["Age"]
                outer["personRow"] = inner
                a << outer
            }

            countries = myData["Query"][1]
            type = countries["Name"]
            countries["Row"].each { |x|
                outer = {}
                inner = {}
                inner["indentifier"] = id
                inner["Type"] = type
                inner["Country"] = x["Country"]
                inner["Population"] = x["Population"]
                outer["countryRow"] = inner
                a << outer
            }

            event.set("AllRows", a)
        '
        remove_field => [ "MyData" ]
    }
    split { field => "AllRows" }
    mutate {
        rename => { "[AllRows][personRow]" => "personRow" "[AllRows][countryRow]" => "countryRow" }
        remove_field => [ "AllRows" ]
    }
2 Likes

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