Ruby plugin remove \ from a string

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 ?

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?

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

mapping it as ruby hash
10x fir the attention

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