Add Execption for special character from json log

I have tried the following and then I dont get any output. As soon as I remove the ruby code the logs display on Kibana or the log file.

filter {
  if [type] == "apache" {

    ruby {
        code => " if event['message']
                  event['message'] = event['message'].gsub('\x','Xx')
                  event['message'] = event['message'].gsub('\\x','XXx')
                  end
                "
            }

    json {
          source => "message"
        }

 }
}