# Extract fields from JSON Flie to Elastic using Logstash filters

**URL:** <https://discuss.elastic.co/t/extract-fields-from-json-flie-to-elastic-using-logstash-filters/274610>\
**Category:** Logstash\
**Created:** [June 1, 2021, 9:51am UTC](https://discuss.elastic.co/t/extract-fields-from-json-flie-to-elastic-using-logstash-filters/274610 "2021-06-01T09:51:02Z")\
**Posts on this page:** 1\
**Showing post:** 4

<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:** [June 1, 2021, 2:17pm UTC](https://discuss.elastic.co/t/extract-fields-from-json-flie-to-elastic-using-logstash-filters/274610/4 "2021-06-01T14:17:59Z")

</div>

> [@Sirine](#):
>
> ```auto
> [{ "start" : 1619731540, "end" : 1619731550, "scen_id" : 0, "conn_id" : 2, "test_id" : 1, "direction" : "fw", "srcip" : "0.0.0.0", "dstip" : "0.0.0.0", "state" : "Atomic Delay", "samples" : 1, "mean_latency" : 261131, "mean_jitter" : 99494, "lost_pkts" : 0, "sent_pkts" : 100, "recv_pkts" : 100}
> ,{ "start" : 1619731540, "end" : 1619731550, "scen_id" : 0, "conn_id" : 2, "test_id" : 1, "direction" : "sw", "srcip" : "0.0.0.0", "dstip" : "0.0.0.0", "state" : "Atomic Delay", "samples" : 1, "mean_latency" : 218259, "mean_jitter" : 21353, "lost_pkts" : 0, "sent_pkts" : 100, "recv_pkts" : 100}
> ]
> 
> ```

Your JSON file contains three lines, none of which are valid JSON. Each of the first two lines is almost valid JSON. There are two approaches you could take, one is to fix up the lines you have using mutate _before_ you try to parse it

```
mutate {
    gsub => [
        "message","\]","",
        "message","\[","",
        "message", "^,", ""
    ]
}
if [message] =~ "^$" { drop {} }
json {
	source => "message"
	target => "event"
}

```

The other is to parse the entire file as an array of JSON objects, then use a split filter to separate them. See [here](https://discuss.elastic.co/t/parsing-array-of-json-objects-with-logstash-and-injesting-to-elastic/203197/2) for more information.

---

_[View the full topic](https://discuss.elastic.co/t/extract-fields-from-json-flie-to-elastic-using-logstash-filters/274610)._
