# Have logstash automatically parse any .json files

**URL:** <https://discuss.elastic.co/t/have-logstash-automatically-parse-any-json-files/241610>\
**Category:** Logstash\
**Created:** [July 17, 2020, 8:31am UTC](https://discuss.elastic.co/t/have-logstash-automatically-parse-any-json-files/241610 "2020-07-17T08:31:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![elk6](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elk6/32/72282_2.png) [@elk6](https://discuss.elastic.co/u/elk6)\
**Post date:** [July 17, 2020, 8:31am UTC](https://discuss.elastic.co/t/have-logstash-automatically-parse-any-json-files/241610/1 "2020-07-17T08:31:54Z")

</div>

I'm planning on shipping lots of logs into logstash. Is it possible to configure logstash in such a way so that only if a file ends with .json it would go through:

```
json {
        source => "message"
      }

```

Thanks ahead!

---

<div class="post-metadata">

**Author:** ![jong99](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@jong99](https://discuss.elastic.co/u/jong99)\
**Post date:** [July 17, 2020, 9:51am UTC](https://discuss.elastic.co/t/have-logstash-automatically-parse-any-json-files/241610/2 "2020-07-17T09:51:53Z")

</div>

You can do this via the file input plugin:

```auto
input {
  file {
    path => "/mydirectory/*.json"
    codec => "json"
  }
}

```

(Or maybe codec json\_lines depending on your file)

Hope this helps!

---

<div class="post-metadata">

**Author:** ![elk6](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elk6/32/72282_2.png) [@elk6](https://discuss.elastic.co/u/elk6)\
**Post date:** [July 17, 2020, 10:07am UTC](https://discuss.elastic.co/t/have-logstash-automatically-parse-any-json-files/241610/3 "2020-07-17T10:07:46Z")

</div>

Thanks for the response!

I actually direct everything to logstash. It just listens for port 5044 for beats. The question is, within the filter{}, what should I put to check if it’s a .json file.

Thanks ahead

---

<div class="post-metadata">

**Author:** ![jong99](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@jong99](https://discuss.elastic.co/u/jong99)\
**Post date:** [July 17, 2020, 10:49am UTC](https://discuss.elastic.co/t/have-logstash-automatically-parse-any-json-files/241610/4 "2020-07-17T10:49:53Z")

</div>

If you're using filebeat you should be able to configure it to only send json files.

If you absolutely need to filter in logstash then the event should have a field with the filename in it (possibly `path`?). You should be able to use an if statement, something along these lines (untested)

```auto
filter {
  if [path] !~ /\.json$/ {
     drop {}
  }
  ...
}

```

Any help?

---

<div class="post-metadata">

**Author:** ![elk6](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elk6/32/72282_2.png) [@elk6](https://discuss.elastic.co/u/elk6)\
**Post date:** [July 17, 2020, 11:08am UTC](https://discuss.elastic.co/t/have-logstash-automatically-parse-any-json-files/241610/5 "2020-07-17T11:08:14Z")

</div>

Thanks for the response,

I’m actually looking to filter it filebeat because I intend to do send all the logs to filebeat. It’s just that some would be JSON.

That if statement would be true if it’s not a .json file, right? If I want the condition to be true, then just replace the ! With = right?

---

<div class="post-metadata">

**Author:** ![jong99](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@jong99](https://discuss.elastic.co/u/jong99)\
**Post date:** [July 17, 2020, 11:11am UTC](https://discuss.elastic.co/t/have-logstash-automatically-parse-any-json-files/241610/6 "2020-07-17T11:11:08Z")

</div>

Ahh right,

You probably then want

```auto
filter {
  if [path] =~ /\.json$/ {
     json {
        source => "message"
     }
  }
  ...
}

```

---

<div class="post-metadata">

**Author:** ![elk6](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elk6/32/72282_2.png) [@elk6](https://discuss.elastic.co/u/elk6)\
**Post date:** [July 17, 2020, 11:55am UTC](https://discuss.elastic.co/t/have-logstash-automatically-parse-any-json-files/241610/7 "2020-07-17T11:55:54Z")

</div>

I’ll try it out, thanks!

Just out of curiousity, do you know why it’s bit \*.json?

---

<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:** [August 14, 2020, 11:56am UTC](https://discuss.elastic.co/t/have-logstash-automatically-parse-any-json-files/241610/8 "2020-08-14T11:56:02Z")

</div>

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