# How to convert to requried JSON

**URL:** <https://discuss.elastic.co/t/how-to-convert-to-requried-json/263254>\
**Category:** Logstash\
**Created:** [February 4, 2021, 2:40pm UTC](https://discuss.elastic.co/t/how-to-convert-to-requried-json/263254 "2021-02-04T14:40:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![onkarborade](https://avatars.discourse-cdn.com/v4/letter/o/8e7dd6/32.png) [@onkarborade](https://discuss.elastic.co/u/onkarborade)\
**Post date:** [February 4, 2021, 2:40pm UTC](https://discuss.elastic.co/t/how-to-convert-to-requried-json/263254/1 "2021-02-04T14:40:17Z")

</div>

Hi,  
Getting following logstash output

```
"rosterentry" : {
                "rosterdaterange" : [
                  "100,01-JUL-20,01-AUG-22",
                  "100,02-JUL-20,02-AUG-22",
                  "101,03-JUL-19,03-AUG-19"
                ],
                "rolerosterid" : [
                  "100",
                  "101"
                ]
              }

```

Output wanted to convert to below format

```
"rosterentry": [
                {
                    "rolerosterid": 100,
                    "rosterdaterange": [
                        {
                            "rolerosterstartdate": "2020-07-01-00:00",
                            "rolerosterenddate": "2022-08-01-00:00"
                        },
                        {
                            "rolerosterstartdate": "2021-07-01-00:00",
                            "rolerosterenddate": "2022-08-01-00:00"
                        }
                    ]
                },
                {
                    "rolerosterid": 101,
                    "rosterdaterange": [
                        {
                            "rolerosterstartdate": "2019-07-01-00:00",
                            "rolerosterenddate": "2019-08-01-00:00"
                        }
                    ]
                }
             ]

```

Logstash configuration file

```
filter {
	mutate {
		split => { "phone_no" => "," }
		split => { "rolerosterid" => "," }
		split => { "rosterdaterange" => "|" }

		# Remove unwanted ,, from rosterdaterange
		gsub => ["rosterdaterange", ",," , ""]

	}
	
	aggregate {
		task_id => "%{sch_id}"
		code => 
		"
			map['sch_id'] = event.get('sch_id') 
			
			map['teachers'] ||= [] 
			map['teachers'] << {
				'tch_id' => event.get('tch_id'),
				'contact_details'=> {
					'phone_no' => event.get('phone_no')
				},

				'rosterentry'=> {
					'rolerosterid' => event.get('rolerosterid'),
					'rosterdaterange'=> event.get('rosterdaterange'),
				}
			}
			event.cancel()
		"
	timeout_tags => ["aggregate"]
	push_previous_map_as_event => true
	timeout => 3
		
	} 
}
```

---

<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:** [March 4, 2021, 2:40pm UTC](https://discuss.elastic.co/t/how-to-convert-to-requried-json/263254/2 "2021-03-04T14:40:52Z")

</div>

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