# How to make multiple lines of json file to single line json file

**URL:** https://discuss.elastic.co/t/how-to-make-multiple-lines-of-json-file-to-single-line-json-file/343424
**Category:** Logstash
**Created:** [September 20, 2023, 7:35am UTC](https://discuss.elastic.co/t/how-to-make-multiple-lines-of-json-file-to-single-line-json-file/343424 "2023-09-20T07:35:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Narayan\_Rao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/narayan_rao/32/124566_2.png) [@Narayan\_Rao](https://discuss.elastic.co/u/Narayan_Rao)
#### Post date: [September 20, 2023, 7:35am UTC](https://discuss.elastic.co/t/how-to-make-multiple-lines-of-json-file-to-single-line-json-file/343424/1 "2023-09-20T07:35:48Z")

</div>

I am having issue with multiple lines json file.

With single line json file able to process the file with below configuration.  
logstash.conf

```auto
input {
	beats{
		port =>	"5044"
		codec => json
	}
}

filter {
   		
	json {
        source => "message"
      }
	mutate {
	 remove_field => ["@version","source"]
	}
	
      mutate { 
	   rename => { "[data][item]" => "item" }
	}
	
	split{
		field => "[item]"
	}

	prune { 
		whitelist_names => "item" 
	}
		

	ruby {
        code => '
			event.get("item").each { |k, v|
			event.set(k,v)
			}
			event.remove("item")
			'
		}
}

output {
	if "_rubyexception" not in [tags]{
		elasticsearch {
			hosts => ["localhost:9200"]
			index => "samplejsonforlogstash5"
			 document_type => "json"
			document_id => "%{id}"
			doc_as_upsert => true
			action => "update"
		}
		 stdout{}
	}
}

```

filebeat.yml

```auto
filebeat.inputs:
- type: log
  paths: C:/sample json files/json format/json.json
output.logstash:
  hosts: ["localhost:5044"]

```

json file

```auto
{"source":"logs","data":{"item":[{"id":"101","crud":"Create","fields":{"partid":"101"},"logistics":{"weight":"6.10","height":"4"}},{"id":"102","crud":"Create","fields":{"partid":"102"},"logistics":{"weight":"0.62","height":"3"}},{"id":"103","crud":"Create","fields":{"partid":"103"},"logistics":{"weight":"4.88","height":"2"}}]}}

```

in this way it is getting stored in logstash. Everything working fine with single line of json.

```auto
[
    {
        "logistics": {
            "weight": "6.10",
            "height": "4"
        },
        "fields": {
            "partid": "101"
        },
        "crud": "Create",
        "id": "101"
    },
    {
        "logistics": {
            "weight": "0.62",
            "height": "3"
        },
        "fields": {
            "partid": "102"
        },
        "crud": "Create",
        "id": "102"
    },
    {
        "logistics": {
            "weight": "4.88",
            "height": "2"
        },
        "fields": {
            "partid": "103"
        },
        "crud": "Create",
        "id": "103"
    }
]

```

Now the issue is i am getting json in multi line, how can i convert to single line json file in logstash. Please help me out to solve this issue.

Just i want to convert multi line to single line json in logstash.

Here is multi line json file

```auto
{
  "source": "logs",
  "data": {
    "item": [
      {
        "id": "101",
        "crud": "Create",
        "fields": {
          "partid": "101"
        },
        "logistics": {
          "weight": "6.10",
          "height": "4"
        }
      },
      {
        "id": "102",
        "crud": "Create",
        "fields": {
          "partid": "102"
        },
        "logistics": {
          "weight": "0.62",
          "height": "3"
        }
      },
      {
        "id": "103",
        "crud": "Create",
        "fields": {
          "partid": "103"
        },
        "logistics": {
          "weight": "4.88",
          "height": "2"
        }
      }
    ]
  }
}

```

---

<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 20, 2023, 6:48pm UTC](https://discuss.elastic.co/t/how-to-make-multiple-lines-of-json-file-to-single-line-json-file/343424/2 "2023-09-20T18:48:37Z")

</div>

> [@Narayan\_Rao](#):
>
> i want to convert multi line to single line json in logstash

See [this](https://discuss.elastic.co/t/exception-logstash-unexpected-character-code-58-expected-a-valid-value-number-string-array-object-true-false-or-null/241198/2) thread.

---

<div class="post-metadata">

### Author: ![Narayan\_Rao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/narayan_rao/32/124566_2.png) [@Narayan\_Rao](https://discuss.elastic.co/u/Narayan_Rao)
#### Post date: [September 21, 2023, 11:29am UTC](https://discuss.elastic.co/t/how-to-make-multiple-lines-of-json-file-to-single-line-json-file/343424/3 "2023-09-21T11:29:08Z")

</div>

@Badger Thanks for reply. Here i am having multi line with multiple events - multiline patter will not work. I just want to make my json content to single line, can you please give some advise how to convert from multiple lines to single line content like below

Single line content  
''''

```auto
{"source":"logs","data":{"item":[{"id":"101","crud":"Create","fields":{"partid":"101"},"logistics":{"weight":"6.10","height":"4"}},{"id":"102","crud":"Create","fields":{"partid":"102"},"logistics":{"weight":"0.62","height":"3"}},{"id":"103","crud":"Create","fields":{"partid":"103"},"logistics":{"weight":"4.88","height":"2"}}]}}

```

---

<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 19, 2023, 11:30am UTC](https://discuss.elastic.co/t/how-to-make-multiple-lines-of-json-file-to-single-line-json-file/343424/4 "2023-10-19T11:30:00Z")

</div>

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