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

**URL:** <https://discuss.elastic.co/t/make-json-flattening-more-dynamic-to-process-nested-json-arrays-objects/200760>\
**Category:** Logstash\
**Created:** [September 23, 2019, 8:54pm UTC](https://discuss.elastic.co/t/make-json-flattening-more-dynamic-to-process-nested-json-arrays-objects/200760 "2019-09-23T20:54:45Z")\
**Posts on this page:** 4\
**Page:** 1

<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:** [September 23, 2019, 8:54pm UTC](https://discuss.elastic.co/t/make-json-flattening-more-dynamic-to-process-nested-json-arrays-objects/200760/1 "2019-09-23T20:54:46Z")

</div>

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

```auto
{
    "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](https://discuss.elastic.co/t/indent-any-json-file-nested-fields-and-make-it-flat/194447/2), 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:

```auto
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:

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

```

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

```auto
{
    "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"
}

```

---

<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:** [September 23, 2019, 9:41pm UTC](https://discuss.elastic.co/t/make-json-flattening-more-dynamic-to-process-nested-json-arrays-objects/200760/2 "2019-09-23T21:41:55Z")

</div>

> [@baselai](#):
>
> it's not what I'm looking for

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

---

<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:** [September 23, 2019, 11:09pm UTC](https://discuss.elastic.co/t/make-json-flattening-more-dynamic-to-process-nested-json-arrays-objects/200760/3 "2019-09-23T23:09:17Z")

</div>

@Badger  
As I mentioned before, the ruby code, **attached up 👆** , 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.

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

```

---

<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:** [October 21, 2019, 11:09pm UTC](https://discuss.elastic.co/t/make-json-flattening-more-dynamic-to-process-nested-json-arrays-objects/200760/4 "2019-10-21T23:09:21Z")

</div>

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