# Ruby plugin remove \\ from a string

**URL:** <https://discuss.elastic.co/t/ruby-plugin-remove-from-a-string/104792>\
**Category:** Logstash\
**Created:** [October 22, 2017, 7:41am UTC](https://discuss.elastic.co/t/ruby-plugin-remove-from-a-string/104792 "2017-10-22T07:41:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yoavsradware](https://avatars.discourse-cdn.com/v4/letter/y/e9c0ed/32.png) [@yoavsradware](https://discuss.elastic.co/u/yoavsradware)\
**Post date:** [October 22, 2017, 7:41am UTC](https://discuss.elastic.co/t/ruby-plugin-remove-from-a-string/104792/1 "2017-10-22T07:41:06Z")

</div>

hey ,  
I am using the logstash ruby plugin  
I am trying to create new json :

```
 ruby {

  init => "require 'json'"
 code => "
      bandwidth = event.get('bandwidth')
      puts bandwidth[0]['count64']
      
     foo = '{\".ssli\": {\\"mappings\\":{\"data\":{\"inspectedbw\" : '+bandwidth[0]['count64'].to_s+' } }}}'
     x=foo.to_s.delete('\\')
     event.set('new',x) 
  "
  }

```

but in the new string there are \ string

```
 {"new":"{\".ssli\": {\"mappings\":{\"data\":{\"inspectedbw\" : 572828245 } }}}"

```

where I would like to get :

```
 {"new":"{".ssli": {"mappings":{"data":{ "inspectedbw" : 572828245 } }}}"

```

note , when I run this simple program :

```
 foo = '{\".ssli\": {\"mappings\":{\"data\":{\"inspectedbw\" : '+1.to_s+' } }}}'
x=foo.to_s.delete('\\\\')

puts(foo.to_s)
 puts(x)

 {\".ssli\": {\"mappings\":{\"data\":{\"inspectedbw\" : 1 } }}}
 {".ssli": {"mappings":{"data":{"inspectedbw" : 1 } }}}

```

where the second one is my requested output whom I would like to see in the event ...

is there another recommended way to do this ?

---

<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:** [October 23, 2017, 5:21am UTC](https://discuss.elastic.co/t/ruby-plugin-remove-from-a-string/104792/2 "2017-10-23T05:21:00Z")

</div>

> I am trying to create new json :

Use the json\_encode filter instead.

> but in the new string there are \ string

Exactly when and where do these backslashes occur?

---

<div class="post-metadata">

**Author:** ![yoavsradware](https://avatars.discourse-cdn.com/v4/letter/y/e9c0ed/32.png) [@yoavsradware](https://discuss.elastic.co/u/yoavsradware)\
**Post date:** [October 23, 2017, 8:11am UTC](https://discuss.elastic.co/t/ruby-plugin-remove-from-a-string/104792/3 "2017-10-23T08:11:29Z")

</div>

i solve it by doing this :  
json = { :mappings=\>  
{ :data=\>  
{  
:timestamp =\> timestamp\_value,  
:inspectedbw=\> inspectedbw} }}

mapping it as ruby hash  
10x fir the attention

---

<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:** [November 20, 2017, 8:11am UTC](https://discuss.elastic.co/t/ruby-plugin-remove-from-a-string/104792/4 "2017-11-20T08:11:35Z")

</div>

Closed on November 20, 2017, 8:11am UTC

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