# Parsing nested json to flat structure

**URL:** <https://discuss.elastic.co/t/parsing-nested-json-to-flat-structure/286790>\
**Category:** Logstash\
**Created:** [October 15, 2021, 3:36am UTC](https://discuss.elastic.co/t/parsing-nested-json-to-flat-structure/286790 "2021-10-15T03:36:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![IsAa](https://avatars.discourse-cdn.com/v4/letter/i/6bbea6/32.png) [@IsAa](https://discuss.elastic.co/u/IsAa)\
**Post date:** [October 15, 2021, 3:36am UTC](https://discuss.elastic.co/t/parsing-nested-json-to-flat-structure/286790/1 "2021-10-15T03:36:19Z")

</div>

Hi Team,

I was looking to parse my nested json structure using json filter. I have looked at some of the posts and tried their solution.

My question, is it possible to make this structure completely flat,

So if my log message is

```auto
{"Common": {"LogLevel":"Info","Details":"Successful","Name":"Test","Browser":"chrome"}, "Application": {"Id":1,"Type":"Web"}}

```

and if I have my filter set up as

```auto
filter {
	json {
         source => "message"
         target => "parsedmessage"
    }
    json {
      source => "[parsedmessage][Application]"
      target => "[parsedmessage][Application]"
    }
json {
      source => "[parsedmessage][Common]"
      target => "[parsedmessage][Common]"
    }
}

```

a) I keep getting error for

```auto
Error parsing json {:source=>"[parsedmessage][Application]", :raw=>{"Id"=>1, "Type"=>"Web"}, :exception=>java.lang.ClassCastException: class org.jruby.RubyHash cannot be cast to class org.jruby.RubyIO (org.jruby.RubyHash and org.jruby.RubyIO are in unnamed module of loader 'app')}

```

b) I am just wondering, if I can make the output as a complete flat structure, like this

```auto
      "@version" => "1",           
             "Id" => 1,
             "Type" => "Web",        
             "Browser" => "chrome",
             "Details" => "Successful",
             "LogLevel" => "Info",
             "Name" => "Test"
       

```

and not some embedded json like this

```auto
      "@version" => "1",
    "parsedmessage" => {
        "Application" => {
              "Id" => 1,
            "Type" => "Web"
        },
             "Common" => {
             "Browser" => "chrome",
             "Details" => "Successful",
            "LogLevel" => "Info",
                "Name" => "Test"
        }

```

Any suggestions, much appreciated.

Thanks

---

<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:** [October 15, 2021, 4:50pm UTC](https://discuss.elastic.co/t/parsing-nested-json-to-flat-structure/286790/2 "2021-10-15T16:50:36Z")

</div>

The json filter will parse all of the nested JSON objects and create hashes from them. The json filter parses a string, which is why you get that error about casting a RubyHash to a RubyIO.

If you want to move the nested objects to the top level and you know their names you can use code like [this](https://discuss.elastic.co/t/how-to-dynamically-move-nested-key-value-to-root-level/180006/2).

---

<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:** [November 12, 2021, 4:51pm UTC](https://discuss.elastic.co/t/parsing-nested-json-to-flat-structure/286790/3 "2021-11-12T16:51:22Z")

</div>

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