# How to parse message with random data using logstash filter?

**URL:** <https://discuss.elastic.co/t/how-to-parse-message-with-random-data-using-logstash-filter/216453>\
**Category:** Logstash\
**Created:** [January 24, 2020, 3:30pm UTC](https://discuss.elastic.co/t/how-to-parse-message-with-random-data-using-logstash-filter/216453 "2020-01-24T15:30:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amit\_Agarwal1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amit_agarwal1/32/57302_2.png) [@Amit\_Agarwal1](https://discuss.elastic.co/u/Amit_Agarwal1)\
**Post date:** [January 24, 2020, 3:30pm UTC](https://discuss.elastic.co/t/how-to-parse-message-with-random-data-using-logstash-filter/216453/1 "2020-01-24T15:30:18Z")

</div>

I have received the below message from the producer at Axon Topic with some random ASCII data.  
"message" =\> "\u0000\u0000\u0000\u0002�\u0004C1�\u0001\u0002�\u0003\u0000\u0000\u0000\u0000A�\u0003{"prdctId":"10077","id":"10077","name":"ComPass","owner":{"id":"E040547","name":"Shashi Raghunandan"},"alias":[{"id":"a1Id","name":"a1Name"},{"id":"a2Id","name":"a2Name"}],"cntryAvail":["n/a"]}�����[\u0002\u0000\u0002\u0000"

I have tried split filter on open curly braces '{' to take out the outer json data from the message.  
mutate {  
split =\> ["message", "{"]  
add\_field =\> { "splitmsg" =\> "%{[message][0]}" }  
add\_field =\> { "msg" =\> "%{[message][1]}" }  
}

but then we get the splitted messages from the message field, which seems not a correct way to do it as after that we got the message in below format.  
{"prdctId":"10077","id":"10077","name":"ComPass","owner":{"id":"E040547","name":"Shashi Raghunandan"},"alias":[{"id":"a1Id","name":"a1Name"},{"id":"a2Id","name":"a2Name"}],"cntryAvail":["n/a"]}

Now, I'm not able to remove backslashes from the message. Please suggest the filter to be applied in such a way to have the output as json from above message into inline format.  
{"prdctId":"10077","id":"10077","name":"ComPass","owner":{"id":"E040547","name":"Shashi Raghunandan"},"alias":[{"id":"a1Id","name":"a1Name"},{"id":"a2Id","name":"a2Name"}],"cntryAvail":["n/a"]}

---

<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 24, 2020, 5:14pm UTC](https://discuss.elastic.co/t/how-to-parse-message-with-random-data-using-logstash-filter/216453/2 "2020-01-24T17:14:40Z")

</div>

You could try something like

```
mutate { gsub => ["message", "^[^{]+", "", "message", "}[^}]+$", "" ] }

```

which would remove everything before the first { and everything after the last }.

---

<div class="post-metadata">

**Author:** ![Amit\_Agarwal1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amit_agarwal1/32/57302_2.png) [@Amit\_Agarwal1](https://discuss.elastic.co/u/Amit_Agarwal1)\
**Post date:** [January 26, 2020, 7:34pm UTC](https://discuss.elastic.co/t/how-to-parse-message-with-random-data-using-logstash-filter/216453/3 "2020-01-26T19:34:23Z")

</div>

After trying above filter, got message as given below.

```
{
    "_index" : "test-product-logstash-22",
    "_type" : "_doc",
    "_id" : "%{id}",
    "_score" : 1.0,
    "_source" : {
      "message" : "{\"prdctId\":\"10077\",\"id\":\"10077\",\"name\":\"ComPass\",\"status\":\"In Development\",\"family\":\"Strategic Growth\",\"line\":\"Strategic Growth\",\"shortDesc\":\"Enables interoperable digital IDs\",\"longDesc\":\"A platform that enables interoperable digital ID's and other capabilities \",\"owner\":{\"id\":\"E040547\",\"name\":\"Shashi Raghunandan\"},\"program\":{\"id\":\"422\",\"name\":\"Regional Solutions and Bundles\"},\"alias\":[{\"id\":\"a1Id\",\"name\":\"a1Name\"},{\"id\":\"a2Id\",\"name\":\"a2Name\"}],\"regAvail\":[\"ap\",\"lac\",\"mea\"],\"cntryAvail\":[\"Global\"],\"salesCenterUrl\":\"salesCenterUrlsfsfd\",\"isActive\":\"true\"",
      "@version" : "1",
      "@timestamp" : "2020-01-26T19:17:49.977Z"
    }
  }

```

But, I wanted to get the message in json format as output.  
{  
"\_index" : "test-product-logstash-22",  
"\_type" : "\_doc",  
"\_id" : "10066",  
"\_score" : 1.0,  
"\_source" : {  
"id" : "10066",  
"prdctId" : "10066",  
"name" : "ComPass",  
"status" : "In Development",  
"family" : "Strategic Growth",  
"line" : "Strategic Growth",  
"shortDesc" : "providers",  
"longDesc" : "population",  
"owner" : {  
"id" : "E040547",  
"name" : "Shashi "  
},  
"program" : {  
"id" : "422",  
"name" : "Regional Solutions and Bundles"  
},  
"alias" : [  
{  
"id" : "995c5dbd-97ee-46bb-b0d8-3e83a097762b",  
"name" : "Community Pass"  
}  
],  
"regAvail" : [  
"ap",  
"lac",  
"mea"  
],  
"cntryAvail" : [  
"n/a"  
],  
"salesCenterUrl" : "n/a",  
"isActive" : "true"  
}  
}

Please suggest the filter needed to be applied to get the output in json format as shown above.

---

<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 26, 2020, 9:10pm UTC](https://discuss.elastic.co/t/how-to-parse-message-with-random-data-using-logstash-filter/216453/4 "2020-01-26T21:10:39Z")

</div>

Use a json filter.

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

---

<div class="post-metadata">

**Author:** ![Amit\_Agarwal1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amit_agarwal1/32/57302_2.png) [@Amit\_Agarwal1](https://discuss.elastic.co/u/Amit_Agarwal1)\
**Post date:** [January 27, 2020, 5:46am UTC](https://discuss.elastic.co/t/how-to-parse-message-with-random-data-using-logstash-filter/216453/5 "2020-01-27T05:46:11Z")

</div>

Please have a look at the output received after applying json filter on message.  
{  
"message" =\> "{"prdctId":"10077","id":"10077","name":"ComPass","status":"In Development","family":"Strategic Growth","line":"Strategic Growth","shortDesc":"Enables interoperable digital IDs","longDesc":"A platform that enables interoperable digital ID's and other capabilities ","owner":{"id":"E040547","name":"Shashi Raghunandan"},"program":{"id":"422","name":"Regional Solutions and Bundles"},"alias":[{"id":"a1Id","name":"a1Name"},{"id":"a2Id","name":"a2Name"}],"regAvail":["ap","lac","mea"],"cntryAvail":["Global"],"salesCenterUrl":"salesCenterUrlsfsfd","isActive":"true"",  
"tags" =\> [  
[0] "\_jsonparsefailure"  
]  
}

Please suggest.

---

<div class="post-metadata">

**Author:** ![Amit\_Agarwal1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amit_agarwal1/32/57302_2.png) [@Amit\_Agarwal1](https://discuss.elastic.co/u/Amit_Agarwal1)\
**Post date:** [January 27, 2020, 6:45am UTC](https://discuss.elastic.co/t/how-to-parse-message-with-random-data-using-logstash-filter/216453/6 "2020-01-27T06:45:41Z")

</div>

Tried the below filter but then missing } at the end which hence not making message structure as json.  
mutate { gsub =\> ["message", "^[^{]+", "", "message", "}[^}]+$", "" ] }

---

<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 27, 2020, 2:33pm UTC](https://discuss.elastic.co/t/how-to-parse-message-with-random-data-using-logstash-filter/216453/7 "2020-01-27T14:33:22Z")

</div>

> [@Amit\_Agarwal1](#):
>
> mutate { gsub =\> ["message", "\[1\]+", "", "message", "}[^}]+$", "" ] }

Try

```
mutate { gsub => ["message", "^[^{]+", "", "message", "[^}]+$", "" ] }

```

* * *

1. ^{

---

<div class="post-metadata">

**Author:** ![Amit\_Agarwal1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amit_agarwal1/32/57302_2.png) [@Amit\_Agarwal1](https://discuss.elastic.co/u/Amit_Agarwal1)\
**Post date:** [January 30, 2020, 5:59am UTC](https://discuss.elastic.co/t/how-to-parse-message-with-random-data-using-logstash-filter/216453/8 "2020-01-30T05:59:22Z")

</div>

Thanks a lot. It's working fine now. After putting json filter, getting the proper structured message in output.

---

<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 27, 2020, 5:59am UTC](https://discuss.elastic.co/t/how-to-parse-message-with-random-data-using-logstash-filter/216453/9 "2020-02-27T05:59:23Z")

</div>

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