# Help please.. to acchive json format

**URL:** <https://discuss.elastic.co/t/help-please-to-acchive-json-format/263330>\
**Category:** Logstash\
**Created:** [February 5, 2021, 5:39am UTC](https://discuss.elastic.co/t/help-please-to-acchive-json-format/263330 "2021-02-05T05:39:07Z")\
**Posts on this page:** 4\
**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 5, 2021, 5:39am UTC](https://discuss.elastic.co/t/help-please-to-acchive-json-format/263330/1 "2021-02-05T05:39:07Z")

</div>

I have rosterentry field as bellow  
`rosterentry = "100,01-JUL-20,01-AUG-22|100,02-JUL-20,02-AUG-22|101,03-JUL-19,03-AUG-19"`

I have converted to :

```
    rosterentry = [
               "100,01-JUL-20,01-AUG-22",
                "100,02-JUL-20,02-AUG-22",
                "101,03-JUL-19,03-AUG-19"
              ]

```

Wants to convert in bellow format:

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

```

Any help will be appreciated

---

<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:** [February 5, 2021, 8:25pm UTC](https://discuss.elastic.co/t/help-please-to-acchive-json-format/263330/2 "2021-02-05T20:25:40Z")

</div>

You could do that with ruby

```
    ruby {
        code => '
            roster = event.get("rosterentry")
            if roster.is_a? Array
                rosterHash = {}
                roster.each { |x|
                    entry = x.split(",")
                    if entry.length == 3
                        id = entry[0]
                        startDate = entry[1]
                        endDate = entry[2]
                        if !rosterHash[id]
                            rosterHash[id] = { "rolerosterid" => id, "rosterdaterange" => [] }
                        end
                        rosterHash[id]["rosterdaterange"] << { "rolerosterstartdate" => startDate, "rolerosterenddate" => endDate }
                    end
                }
            end
            rosterArray = []
            rosterHash.each { |k, v|
                rosterArray << v
            }
            event.set("rosterentry", rosterArray)
        '
    }
```

---

<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 8, 2021, 5:06am UTC](https://discuss.elastic.co/t/help-please-to-acchive-json-format/263330/3 "2021-02-08T05:06:05Z")

</div>

Thank you @Badger

---

<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 8, 2021, 5:06am UTC](https://discuss.elastic.co/t/help-please-to-acchive-json-format/263330/4 "2021-03-08T05:06:27Z")

</div>

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