# Parse JSON input file, extract fields and write them in output file

**URL:** <https://discuss.elastic.co/t/parse-json-input-file-extract-fields-and-write-them-in-output-file/261543>\
**Category:** Logstash\
**Created:** [January 19, 2021, 12:37pm UTC](https://discuss.elastic.co/t/parse-json-input-file-extract-fields-and-write-them-in-output-file/261543 "2021-01-19T12:37:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fantonio](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fantonio/32/82537_2.png) [@fantonio](https://discuss.elastic.co/u/fantonio)\
**Post date:** [January 19, 2021, 12:37pm UTC](https://discuss.elastic.co/t/parse-json-input-file-extract-fields-and-write-them-in-output-file/261543/1 "2021-01-19T12:37:45Z")

</div>

I have an input file where each line is a JSON object like the following:

```
      {
         "status_code" => "200",
          "method" => "GET",
          "country" => "US",
           "field" => "something",
        }

```

The goal is (for each JSON record) to extract all the fields values and write them in an output file as a single line:

```
 "200" "GET" "US" "something" ----> object 1
 "404" "GET" "IT" "something_2" -----> object 2

```

This is my conf:

```
input {
  file {
    path => "path/to/input.json"
    codec => "json"
    start_position => "beginning"
  }
}

filter {
  json {
    source => "message"
  }

  mutate {
    add_field => {
      "status_code" => "%{[status_code]}"
      "method" => "%{[method]}"
      "country" => "%{[country]}"
      "field" => "%{[field]}"
    }
  }

  mutate {
    remove_field => ["message"]
  }

}

output {
  file {
     path => "path/to/output.txt"
     codec => line { format => "%{[status_code]} %{[method]} %{[country]} %{[field]}" }
  }
}

```

which results in

`%{[status_code]} %{[method]} %{[country]} %{[field]}`

Any suggestion about that?  
Thanks

---

<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 19, 2021, 4:29pm UTC](https://discuss.elastic.co/t/parse-json-input-file-extract-fields-and-write-them-in-output-file/261543/2 "2021-01-19T16:29:34Z")

</div>

For a file input a path must be absolute. A relative path like `"path/to/input.json"` will result in an error.

The example JSON you show is 6 lines, not one line, so neither codec nor filter will parse it. If it is really spread across 6 lines you will need to use a multiline codec to combine them as a single event.

If you have a JSON object on a single line then a json codec will parse it. In that case there will be no [message] field unless the JSON object contains one. So unless that is true the json filter will be a no-op.

```
mutate { add_field => { "status_code" => "%{[status_code]}" } }

```

This filter adds a [status\_code] field equal to the current value of the [status\_code] field (as a side effect it converts it to a string value if it is anything else). If the [status\_code] field does not exist it sets the [status\_code] field to "%{[status\_code]}". It seems unlikely that either of these are what you want to happen.

---

<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 16, 2021, 4:29pm UTC](https://discuss.elastic.co/t/parse-json-input-file-extract-fields-and-write-them-in-output-file/261543/3 "2021-02-16T16:29:39Z")

</div>

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