# Parse json and non-json logs

**URL:** <https://discuss.elastic.co/t/parse-json-and-non-json-logs/324282>\
**Category:** Logstash\
**Created:** [January 31, 2023, 1:35am UTC](https://discuss.elastic.co/t/parse-json-and-non-json-logs/324282 "2023-01-31T01:35:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![true64gurus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/true64gurus/32/107237_2.png) [@true64gurus](https://discuss.elastic.co/u/true64gurus)\
**Post date:** [January 31, 2023, 1:35am UTC](https://discuss.elastic.co/t/parse-json-and-non-json-logs/324282/1 "2023-01-31T01:35:00Z")

</div>

I have multiple Kubernetes clusters sending logs to S3 , then I use logstash to read logs off S3 files. Some logs messages are in JSON , others in structured format.

How to dynamically detect and parse JSON , and save non-JSON as it is. I use this filter but not working.

```auto
filter {
  if [message] =~ "\A\{.+\}\z" {
    json {
        source => "message"
      }
    mutate { add_field => { "json_detected" => "This is json log" } }
  }
}

```

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:** [January 31, 2023, 2:52am UTC](https://discuss.elastic.co/t/parse-json-and-non-json-logs/324282/2 "2023-01-31T02:52:29Z")

</div>

You could try always using a json filter. If you get a \_jsonparsefailure then it is not JSON, so process it as a structured log.

Unless your structured logs look a lot like JSON it is going to be pretty cheap for the json filter to notice that [message] does not start with a {

---

<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:** [February 28, 2023, 2:52am UTC](https://discuss.elastic.co/t/parse-json-and-non-json-logs/324282/3 "2023-02-28T02:52:32Z")

</div>

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