# Error parsing JSON file

**URL:** <https://discuss.elastic.co/t/error-parsing-json-file/244014>\
**Category:** Logstash\
**Created:** [August 6, 2020, 11:38am UTC](https://discuss.elastic.co/t/error-parsing-json-file/244014 "2020-08-06T11:38:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vikash\_Singh1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vikash_singh1/32/42119_2.png) [@Vikash\_Singh1](https://discuss.elastic.co/u/Vikash_Singh1)\
**Post date:** [August 6, 2020, 11:38am UTC](https://discuss.elastic.co/t/error-parsing-json-file/244014/1 "2020-08-06T11:38:33Z")

</div>

I am trying to parse a json file which consists of multiple line but even though I am using multiline codec but still I am getting error. Sample of my Json file is as follows:

```
> [
  {
    "action": "drop",
    "clientASNDescription": "TRUE-AS-AP True Internet Co.,Ltd.",
    "clientAsn": "17552",
    "clientCountryName": "TH",
    "clientIP": "xxx.xxx.xx.xx",
    "clientRequestHTTPHost": "www.abc.com",
    "clientRequestHTTPMethodName": "GET",
    "clientRequestHTTPProtocol": "HTTP/1.1",
    "clientRequestPath": "/xx/xx/xxx/xxx",
    "clientRequestQuery": "",
    "datetime": "2020-08-02T13:30:00Z",
    "rayName": "6avbcghnasj",
    "ruleId": "defcc1554sadsadsad",
    "source": "firewallrules",
    "userAgent": "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
    "matchIndex": 0,
    "metadata": [
      {
        "key": "filter",
        "value": "asdasdsa2asd12a1s221asd"
      },
      {
        "key": "type",
        "value": "abcdef"
      }
    ],
    "sampleInterval": 1
  }
]
My grok is as follow:

input {
file {
#type => "json"
path => "/home/kagamee/Downloads/jetairways_logs/*.json"
codec => multiline {pattern => '^\{' negate => true what => "previous" auto_flush_interval => 2 max_lines => 100000000 }
start_position => "beginning"
sincedb_path => "/dev/null"
}
}
filter
{
    mutate
    {
        replace => ["message", "%{message}}"]
        gsub => ['message','\n','']
    }
    if [message] =~ /^{.*}$/
    {
        json { source => message }
    }

}
output {
stdout { codec => rubydebug }
}

```

Thanks in advance

---

<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:** [August 6, 2020, 1:45pm UTC](https://discuss.elastic.co/t/error-parsing-json-file/244014/2 "2020-08-06T13:45:08Z")

</div>

Your JSON will include the closing ], so you need to gsub that off of [message] before trying to parse it.

---

<div class="post-metadata">

**Author:** ![Vikash\_Singh1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vikash_singh1/32/42119_2.png) [@Vikash\_Singh1](https://discuss.elastic.co/u/Vikash_Singh1)\
**Post date:** [August 7, 2020, 5:30am UTC](https://discuss.elastic.co/t/error-parsing-json-file/244014/3 "2020-08-07T05:30:07Z")

</div>

I did some changes in the conf file but still not getting correct output

```
> input {
file {
#type => "json"
path => "/home/kagamee/Downloads/abc.json"
codec => multiline {pattern => '[{^\}]' negate => true what => "previous" auto_flush_interval => 2 max_lines => 100000000 }
start_position => "beginning"
sincedb_path => "/dev/null"
}
}
filter
{
    mutate
    {
        replace => ["message", "%{message}}"]
        gsub => ['message','\n',' ', 'message','{',' ']
    }
    if [message] =~ /^{.*}$/       
    {
        json { source => message }
    }

}
output {
stdout { codec => rubydebug }
}

```

The output is as follows:

> "message" =\> " "action": "adwchallenge", "clientASNDescription": "XXX", "clientAsn": "1111", "clientCountryName": "KZ", "clientIP": "xx.xx.x.Xx", "clientRequestHTTPHost": "abcd.com", "clientRequestHTTPMethodName": "GET", "clientRequestHTTPProtocol": "HTTP/2", "clientRequestPath": "/abc/Jan19/xxx.gif", "clientRequestQuery": "", "datetime": "2020-08-03T14:52:34Z", "rayName": "aabbcccd", "ruleId": "56i", "source": "securitylevel", "userAgent": "Microsoft Office/16.0 (Microsoft Outlook 16.0.12827; Pro), Mozilla/4.0 (compatible; ms-office; MSOffice 16)", "matchIndex": 0, "metadata": , "sampleInterval": 1}"  
> }

---

<div class="post-metadata">

**Author:** ![Jenni](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jenni/32/29684_2.png) [@Jenni](https://discuss.elastic.co/u/Jenni)\
**Post date:** [August 7, 2020, 6:15am UTC](https://discuss.elastic.co/t/error-parsing-json-file/244014/4 "2020-08-07T06:15:57Z")

</div>

Now you replaced your opening curly brackets with a space, so it is invalid JSON and the regex in your condition won't match?!

---

<div class="post-metadata">

**Author:** ![Vikash\_Singh1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vikash_singh1/32/42119_2.png) [@Vikash\_Singh1](https://discuss.elastic.co/u/Vikash_Singh1)\
**Post date:** [August 7, 2020, 6:53am UTC](https://discuss.elastic.co/t/error-parsing-json-file/244014/5 "2020-08-07T06:53:02Z")

</div>

I noticed that I have removed it with ']' but still getting same ouput.

---

<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:** [September 4, 2020, 6:53am UTC](https://discuss.elastic.co/t/error-parsing-json-file/244014/6 "2020-09-04T06:53:04Z")

</div>

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