# Using mutate to parse broken JSON

**URL:** <https://discuss.elastic.co/t/using-mutate-to-parse-broken-json/314436>\
**Category:** Logstash\
**Created:** [September 14, 2022, 3:43pm UTC](https://discuss.elastic.co/t/using-mutate-to-parse-broken-json/314436 "2022-09-14T15:43:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan\_Fielder](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dan_fielder/32/110895_2.png) [@Dan\_Fielder](https://discuss.elastic.co/u/Dan_Fielder)\
**Post date:** [September 14, 2022, 3:43pm UTC](https://discuss.elastic.co/t/using-mutate-to-parse-broken-json/314436/1 "2022-09-14T15:43:09Z")

</div>

I have messages arriving with very large blocks of broken JSON. These logs fail to parse when they arrive, so I'm using Filebeat to read from logstash-plain.log and feed them back in for extra processing. Each log contains 30+ individual events separated by commas, but the first and last event are getting clipped somewhere in the pipeline. I was hoping to use the mutate plugin to gsub a unique sequence between each event, then use the split function to break them apart into individual messages before sending those back through the JSON parser for processing. I realize the first and last events are unusable, but at least I would get the 30 or so in the middle.

The messages arriving look like this:  
tamp\": 124386584932940, \"host\": \"host.sys.name\" , \"name\":\"name\" } , { \"timestamp\": 124386584932940, \"host\": \"host.sys.name\" , \"name\":\"name\" } , { \"timestamp\": 124386584932940, \"host\": \"host.sys.name\" , \"name\":\"name\" } , (repeats 30+ times)

There are commas and spaces all over these logs, so I have to use the entire string } , { to target the comma between events. I need to preserve the brackets on either side so the JSON still works and I need to change the comma to something unique I can use to split later.

To that end, I set up the following code block:

```auto
mutate {
  gsub => [
    "message", "} , {", "}XX.XX{"
  ]
}

```

When my logs run through this function, instead of replacing } , { with }XX.XX{ it replaces with }, {

I know that both } and , are special characters in conf files, but when I try to escape those with \ the results are still the same. What's the correct format for this function to work?

---

<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:** [October 12, 2022, 3:43pm UTC](https://discuss.elastic.co/t/using-mutate-to-parse-broken-json/314436/2 "2022-10-12T15:43:32Z")

</div>

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