# Check that a message is in JSON to avoid \_jsonparsefailure

**URL:** <https://discuss.elastic.co/t/check-that-a-message-is-in-json-to-avoid--jsonparsefailure/54959>\
**Category:** Logstash\
**Created:** [July 7, 2016, 3:08pm UTC](https://discuss.elastic.co/t/check-that-a-message-is-in-json-to-avoid--jsonparsefailure/54959 "2016-07-07T15:08:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![norandom](https://avatars.discourse-cdn.com/v4/letter/n/d07c76/32.png) [@norandom](https://discuss.elastic.co/u/norandom)\
**Post date:** [July 7, 2016, 3:08pm UTC](https://discuss.elastic.co/t/check-that-a-message-is-in-json-to-avoid--jsonparsefailure/54959/1 "2016-07-07T15:08:58Z")

</div>

Hi,

I have a logfile, which sadly is mixed. There are JSON lines, and there are free text lines. The good news is, that there is always a line break and JSON parsing works.

Currently I do:

```
   if "_jsonparsefailure" in [tags] {
                    drop { }
            }

```

I'd like to have an earlier check like

```
if ( [mesage] =~ ({.*}) ) {
   json {
                        source => "message"
                }
}

```

- Just using the curly braces to qualify for parsing

Otherwise I'd directly output it into Elasticsearch. Is there a simple way to do this, to save some performance and to avoid regular parser failures?

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [July 8, 2016, 5:44am UTC](https://discuss.elastic.co/t/check-that-a-message-is-in-json-to-avoid--jsonparsefailure/54959/2 "2016-07-08T05:44:30Z")

</div>

Sure, you can do that. The conditional in your example needs a few adjustments but the idea is right.

```
if [message] =~ /^{.*}$/ {
```

---

<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 6, 2017, 4:48am UTC](https://discuss.elastic.co/t/check-that-a-message-is-in-json-to-avoid--jsonparsefailure/54959/3 "2017-07-06T04:48:57Z")

</div>


