# Parsing JSON with a string at the beginning of each JSON Object

**URL:** https://discuss.elastic.co/t/parsing-json-with-a-string-at-the-beginning-of-each-json-object/285480
**Category:** Logstash
**Created:** [September 29, 2021, 1:24pm UTC](https://discuss.elastic.co/t/parsing-json-with-a-string-at-the-beginning-of-each-json-object/285480 "2021-09-29T13:24:56Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![pkward](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pkward/32/95305_2.png) [@pkward](https://discuss.elastic.co/u/pkward)
#### Post date: [September 29, 2021, 1:24pm UTC](https://discuss.elastic.co/t/parsing-json-with-a-string-at-the-beginning-of-each-json-object/285480/1 "2021-09-29T13:24:56Z")

</div>

Hello,

I'm trying to parse a json file with a string at the beginning of each object. What is the easiest way to bypass the initial string or parse the json with string at the beginning?

Here is an example of the JSON:

string {"field": "value"}

---

<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: [September 29, 2021, 2:00pm UTC](https://discuss.elastic.co/t/parsing-json-with-a-string-at-the-beginning-of-each-json-object/285480/2 "2021-09-29T14:00:13Z")

</div>

Use mutate+gsub to remove the string.

---

<div class="post-metadata">

### Author: ![pkward](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pkward/32/95305_2.png) [@pkward](https://discuss.elastic.co/u/pkward)
#### Post date: [September 29, 2021, 2:15pm UTC](https://discuss.elastic.co/t/parsing-json-with-a-string-at-the-beginning-of-each-json-object/285480/3 "2021-09-29T14:15:46Z")

</div>

Thanks @Badger. Second question would the string be considered the source or message?

---

<div class="post-metadata">

### Author: ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)
#### Post date: [September 29, 2021, 2:23pm UTC](https://discuss.elastic.co/t/parsing-json-with-a-string-at-the-beginning-of-each-json-object/285480/4 "2021-09-29T14:23:22Z")

</div>

```auto
filter {
  mutate {
    gsub => ["message", "string ", ""]
  }  
  json {
    source => "message"
  }  
}

```

---

<div class="post-metadata">

### Author: ![pkward](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pkward/32/95305_2.png) [@pkward](https://discuss.elastic.co/u/pkward)
#### Post date: [September 29, 2021, 2:27pm UTC](https://discuss.elastic.co/t/parsing-json-with-a-string-at-the-beginning-of-each-json-object/285480/5 "2021-09-29T14:27:59Z")

</div>

Ok, I have something similar. To give context the strings are domain names and they're all different. I'm trying to use regex, but every domain is different including numbers with text, just numbers, or just text.

---

<div class="post-metadata">

### Author: ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)
#### Post date: [September 29, 2021, 2:34pm UTC](https://discuss.elastic.co/t/parsing-json-with-a-string-at-the-beginning-of-each-json-object/285480/6 "2021-09-29T14:34:58Z")

</div>

If there is always a space after the text `domainname {` then an easy option is to just grok after that first space.

```auto
filter {
  grok {
    match => { "message" => " %{GREEDYDATA:new_message}" }
  }
  json {
    source => "new_message"
  }
  mutate {
    remove_field => ["new_message", "message"]
  }  
}

```

**Output**

```auto
{
    "@timestamp" => 2021-09-29T14:33:23.008Z,
         "field" => "value"
}

```

---

<div class="post-metadata">

### Author: ![pkward](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pkward/32/95305_2.png) [@pkward](https://discuss.elastic.co/u/pkward)
#### Post date: [September 29, 2021, 2:42pm UTC](https://discuss.elastic.co/t/parsing-json-with-a-string-at-the-beginning-of-each-json-object/285480/7 "2021-09-29T14:42:56Z")

</div>

I'm giving it a try right now.

---

<div class="post-metadata">

### Author: ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)
#### Post date: [September 29, 2021, 2:57pm UTC](https://discuss.elastic.co/t/parsing-json-with-a-string-at-the-beginning-of-each-json-object/285480/8 "2021-09-29T14:57:03Z")

</div>

If you always have this format:

```auto
string1 {json}
string2 {json}
string3 {json}

```

You could also use a dissect filter to split your message in two parts.

```auto
dissect {
    mapping => {
        "message" => "%{domainName} %{jsonData}"
    }
}

```

So for the following example:

```auto
string1 {"field": "value"}

```

This dissect filter will create two fields:

```auto
domainName: "string1"
jsonData: {"field": "value"}

```

Then you can use the `json` fitler with the `jsonData` field.

This is similar to the `grok` example, but `dissect` uses less CPU.

---

<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 27, 2021, 2:57pm UTC](https://discuss.elastic.co/t/parsing-json-with-a-string-at-the-beginning-of-each-json-object/285480/9 "2021-10-27T14:57:39Z")

</div>

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