# How to parser the json object dynamically in logstsh using ruby code

**URL:** https://discuss.elastic.co/t/how-to-parser-the-json-object-dynamically-in-logstsh-using-ruby-code/146027
**Category:** Logstash
**Created:** [August 25, 2018, 3:30pm UTC](https://discuss.elastic.co/t/how-to-parser-the-json-object-dynamically-in-logstsh-using-ruby-code/146027 "2018-08-25T15:30:10Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Charan\_Adabala](https://avatars.discourse-cdn.com/v4/letter/c/eb8c5e/32.png) [@Charan\_Adabala](https://discuss.elastic.co/u/Charan_Adabala)
#### Post date: [August 25, 2018, 3:30pm UTC](https://discuss.elastic.co/t/how-to-parser-the-json-object-dynamically-in-logstsh-using-ruby-code/146027/1 "2018-08-25T15:30:11Z")

</div>

I have this as input, I need to parse all the keys and values and put in a sperate event, can you please help me how to achive, Catually I'm only one key and value when I using the below ruby code.

"entities": {  
"PROCESS\_GROUP\_INSTANCE-EA653E6874E21338": "WebSphere AS nzapwa145Cell (nzapwa146 / ePIMS\_pwa146)",  
"PROCESS\_GROUP\_INSTANCE-64A2967FB7FC0C5B": "WebSphere AS ndmProd (nzapwa127 / ePIMS\_Report\_pwa127)",  
"PROCESS\_GROUP\_INSTANCE-4738C9392BDBC296": "EPS Store - 900008014",  
"PROCESS\_GROUP\_INSTANCE-BA196B9B53FF6323": "EPS Store - 900008040",  
"PROCESS\_GROUP\_INSTANCE-D5D2DAB06C8FDAAF": "ws-server.jar prod-sj-userprofile-adapter-service-_-_-\*",  
"PROCESS\_GROUP\_INSTANCE-3EABAD79933CE911": "/apps/conf/httpd-p2\_cdis.conf - KPATHS",  
}

Code:  
ruby {  
code =\> '  
event.get("[result][entities]").each { |key, value|  
event.set("hostId", key)  
event.set("serverName", value)  
}  
'  
}

Output for above code:  
{"serverName":"/apps/conf/httpd-p2\_cdis.conf - KPATHS)","@timestamp":"2018-08-25T15:17:11.762Z","hostId":"PROCESS\_GROUP\_INSTANCE-3EABAD79933CE911"}

But desired output will be like this :

{"serverName":"WebSphere AS nzapwa145Cell (nzapwa146 / ePIMS\_pwa146)","@timestamp":"2018-08-25T15:17:11.762Z","hostId":"PROCESS\_GROUP\_INSTANCE-EA653E6874E21338"}  
{"serverName":"WebSphere AS ndmProd (nzapwa127 / ePIMS\_Report\_pwa127)","@timestamp":"2018-08-25T15:17:11.762Z","hostId":"PROCESS\_GROUP\_INSTANCE-64A2967FB7FC0C5B"}  
{"serverName":"EPS Store - 900008014","@timestamp":"2018-08-25T15:17:11.762Z","hostId":"PROCESS\_GROUP\_INSTANCE-4738C9392BDBC296"}  
{"serverName":"ws-server.jar prod-sj-userprofile-adapter-service-_-_-\*","@timestamp":"2018-08-25T15:17:11.762Z","hostId":"PROCESS\_GROUP\_INSTANCE-BA196B9B53FF6323"}  
{"serverName":"/apps/conf/httpd-p2\_cdis.conf - KPATHS","@timestamp":"2018-08-25T15:17:11.762Z","hostId":"PROCESS\_GROUP\_INSTANCE-D5D2DAB06C8FDAAF"}  
{"serverName":"/apps/conf/httpd-p2\_cdis.conf - KPATHS)","@timestamp":"2018-08-25T15:17:11.762Z","hostId":"PROCESS\_GROUP\_INSTANCE-3EABAD79933CE911"}

Can you please help me to get the solution, I'm facing problem with ruby code.

---

<div class="post-metadata">

### Author: ![Charan\_Adabala](https://avatars.discourse-cdn.com/v4/letter/c/eb8c5e/32.png) [@Charan\_Adabala](https://discuss.elastic.co/u/Charan_Adabala)
#### Post date: [August 25, 2018, 6:16pm UTC](https://discuss.elastic.co/t/how-to-parser-the-json-object-dynamically-in-logstsh-using-ruby-code/146027/2 "2018-08-25T18:16:20Z")

</div>

Can you please help any one

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [August 27, 2018, 6:13am UTC](https://discuss.elastic.co/t/how-to-parser-the-json-object-dynamically-in-logstsh-using-ruby-code/146027/3 "2018-08-27T06:13:26Z")

</div>

You'll want to use the ruby code to produce an array field like this:

```nohighlight
{
  "some-field": [
    {"serverName":"WebSphere AS nzapwa145Cell (nzapwa146 / ePIMS_pwa146)","@timestamp":"2018-08-25T15:17:11.762Z","hostId":"PROCESS_GROUP_INSTANCE-EA653E6874E21338"},
    {"serverName":"WebSphere AS ndmProd (nzapwa127 / ePIMS_Report_pwa127)","@timestamp":"2018-08-25T15:17:11.762Z","hostId":"PROCESS_GROUP_INSTANCE-64A2967FB7FC0C5B"},
    ...
  ]
}

```

Then you can use a split filter on the array field to put each the field in each array element into an event of its own.

---

<div class="post-metadata">

### Author: ![Charan\_Adabala](https://avatars.discourse-cdn.com/v4/letter/c/eb8c5e/32.png) [@Charan\_Adabala](https://discuss.elastic.co/u/Charan_Adabala)
#### Post date: [September 17, 2018, 1:44pm UTC](https://discuss.elastic.co/t/how-to-parser-the-json-object-dynamically-in-logstsh-using-ruby-code/146027/4 "2018-09-17T13:44:20Z")

</div>

Can you please help how to produce an array for the above explanation, Please help me

---

<div class="post-metadata">

### Author: ![Charan\_Adabala](https://avatars.discourse-cdn.com/v4/letter/c/eb8c5e/32.png) [@Charan\_Adabala](https://discuss.elastic.co/u/Charan_Adabala)
#### Post date: [September 18, 2018, 12:45pm UTC](https://discuss.elastic.co/t/how-to-parser-the-json-object-dynamically-in-logstsh-using-ruby-code/146027/5 "2018-09-18T12:45:03Z")

</div>

Can you help me please

---

<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 16, 2018, 12:45pm UTC](https://discuss.elastic.co/t/how-to-parser-the-json-object-dynamically-in-logstsh-using-ruby-code/146027/6 "2018-10-16T12:45:08Z")

</div>

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