# Flatten nested json array

**URL:** <https://discuss.elastic.co/t/flatten-nested-json-array/196192>\
**Category:** Logstash\
**Created:** [August 21, 2019, 9:00pm UTC](https://discuss.elastic.co/t/flatten-nested-json-array/196192 "2019-08-21T21:00:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rokitpower](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@rokitpower](https://discuss.elastic.co/u/rokitpower)\
**Post date:** [August 21, 2019, 9:00pm UTC](https://discuss.elastic.co/t/flatten-nested-json-array/196192/1 "2019-08-21T21:00:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![baselai](https://avatars.discourse-cdn.com/v4/letter/b/ea666f/32.png) [@baselai](https://discuss.elastic.co/u/baselai)\
**Post date:** [August 21, 2019, 9:26pm UTC](https://discuss.elastic.co/t/flatten-nested-json-array/196192/2 "2019-08-21T21:26:03Z")

</div>

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

> [@Indent any json file nested fields and make it flat](https://discuss.elastic.co/t/indent-any-json-file-nested-fields-and-make-it-flat/194447/2):
>
> You will need a ruby script. Create a file called flattenJSON.rb that contains 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) […

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [August 21, 2019, 9:34pm UTC](https://discuss.elastic.co/t/flatten-nested-json-array/196192/3 "2019-08-21T21:34:07Z")

</div>

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"]
    }
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 18, 2019, 9:34pm UTC](https://discuss.elastic.co/t/flatten-nested-json-array/196192/4 "2019-09-18T21:34:10Z")

</div>

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