# Set root for json codec

**URL:** <https://discuss.elastic.co/t/set-root-for-json-codec/185906>\
**Category:** Logstash\
**Created:** [June 14, 2019, 7:59pm UTC](https://discuss.elastic.co/t/set-root-for-json-codec/185906 "2019-06-14T19:59:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nate\_McCallum](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nate_mccallum/32/48163_2.png) [@Nate\_McCallum](https://discuss.elastic.co/u/Nate_McCallum)\
**Post date:** [June 14, 2019, 7:59pm UTC](https://discuss.elastic.co/t/set-root-for-json-codec/185906/1 "2019-06-14T19:59:10Z")

</div>

Using the json\_codec on input I get all my fields parsed at the root level. I like to specify a name for the subfield for the items.  
So right now with { "a" : "b", "c" : "d" } to get a = b and c = d. I'd like to get myname.a = b and myname.c = d.

I'm concerned about doing this in a ruby filter as I saw a post that the json\_codec is much faster than a ruby filter.

Any thoughts?

---

<div class="post-metadata">

**Author:** ![yaauie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yaauie/32/23363_2.png) [@yaauie](https://discuss.elastic.co/u/yaauie)\
**Post date:** [June 14, 2019, 11:19pm UTC](https://discuss.elastic.co/t/set-root-for-json-codec/185906/2 "2019-06-14T23:19:07Z")

</div>

The JSON codec does not support this behaviour.

Your best bet is likely to use the [JSON filter](https://www.elastic.co/guide/en/logstash/current/plugins-filters-json.html), which can take the value of a JSON string field, parse it, and place the result somewhere. Assuming you use your input's default codec and the raw contents are placed into `message`, this would do the trick:

```auto
filter {
  json {
    source => "message"
    target => "myname"
    remove_field => "message"
  }
}

```

The `remove_field` directive is optional and is only invoked on success.

---

<div class="post-metadata">

**Author:** ![Nate\_McCallum](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nate_mccallum/32/48163_2.png) [@Nate\_McCallum](https://discuss.elastic.co/u/Nate_McCallum)\
**Post date:** [June 15, 2019, 10:14am UTC](https://discuss.elastic.co/t/set-root-for-json-codec/185906/3 "2019-06-15T10:14:13Z")

</div>

Thanks Ry, The problem I'm running into with that solution is the message maybe be an array of json objects. [{"a":"b"},{"a":"c"}]. In this case json\_codec handles properly but json filter is unhappy as the entire array came in as a single message / document. Any ideas to handle the array format?

Again thank you for the quick response.

---

<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:** [June 15, 2019, 1:06pm UTC](https://discuss.elastic.co/t/set-root-for-json-codec/185906/4 "2019-06-15T13:06:46Z")

</div>

The json filter will handle an array provided that you use the target option.

---

<div class="post-metadata">

**Author:** ![Nate\_McCallum](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nate_mccallum/32/48163_2.png) [@Nate\_McCallum](https://discuss.elastic.co/u/Nate_McCallum)\
**Post date:** [June 17, 2019, 1:19pm UTC](https://discuss.elastic.co/t/set-root-for-json-codec/185906/5 "2019-06-17T13:19:10Z")

</div>

Thanks Badger. What I am seeing is this:

With this json filter:

json {

skip\_on\_invalid\_json =\> true

source =\> "message"

target =\> "mytarget"

remove\_field =\> ["message"]

}

I get this as my document.

 ![image.png](https://us1.discourse-cdn.com/elastic/original/3X/f/b/fb88d6f5b8dbc41205de51943733e2856106ec2a.png)

Am I implementing the filter incorrectly to get two documents?

---

<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:** [June 17, 2019, 1:24pm UTC](https://discuss.elastic.co/t/set-root-for-json-codec/185906/6 "2019-06-17T13:24:00Z")

</div>

If you want two documents then pass the array to a split filter.

---

<div class="post-metadata">

**Author:** ![Nate\_McCallum](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nate_mccallum/32/48163_2.png) [@Nate\_McCallum](https://discuss.elastic.co/u/Nate_McCallum)\
**Post date:** [June 17, 2019, 5:33pm UTC](https://discuss.elastic.co/t/set-root-for-json-codec/185906/7 "2019-06-17T17:33:09Z")

</div>

Thanks Badger. I ended up with this in my filter to get the desired result.

```auto
    json {
      skip_on_invalid_json => true
      source => "message"
      target => "myfield"
      remove_field => ["message"]
    }

    # If the data is in an array then add a tag to allow for splitting each index into own event.
    ruby {
      code => "if event.get('myfield').is_a? Hash
                event.tag('ishash')
               end"
    }

    # if isHash is not set then go ahead and split without this check any fields that aren't arrays throw WARN.
    if "ishash" not in [tags] {
      split {
        field => "myfield"
      }
    }
```

---

<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:** [July 15, 2019, 5:33pm UTC](https://discuss.elastic.co/t/set-root-for-json-codec/185906/8 "2019-07-15T17:33:10Z")

</div>

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