# Painless code in watcher for replacing doble quotes

**URL:** <https://discuss.elastic.co/t/painless-code-in-watcher-for-replacing-doble-quotes/344508>\
**Category:** Elastic Observability\
**Tags:** painless\
**Created:** [October 5, 2023, 9:23pm UTC](https://discuss.elastic.co/t/painless-code-in-watcher-for-replacing-doble-quotes/344508 "2023-10-05T21:23:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![juanmgarciaf](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@juanmgarciaf](https://discuss.elastic.co/u/juanmgarciaf)\
**Post date:** [October 5, 2023, 9:23pm UTC](https://discuss.elastic.co/t/painless-code-in-watcher-for-replacing-doble-quotes/344508/1 "2023-10-05T21:23:07Z")

</div>

Hello,

```
 I have a problem using painless code in a watcher. I need to replace all the double quotes characters and put a single quote instead, but I don't know how I can do this. After read the Elastic documentation I put the following in the code:

```

```auto
"transform": {
    "script": {
      "source": """
            return ctx.payload.hits.hits.stream()
           .map(hit -> {
             def last = hit.inner_hits.test_cases.hits.hits[0];
             def errorMessage = last.fields['error.message']?.0;
             errorMessage = /[\"]/.matcher(errorMessage).replaceAll('\'');

```

But I obtain this error:

"transform": {  
"type": "script",  
"status": "failure",  
"reason": "runtime error",  
"error": {  
"root\_cause": [  
{  
"type": "script\_exception",  
"reason": "runtime error",  
"script\_stack": [  
"errorMessage = /[\"]/.matcher(errorMessage).replaceAll('\'');\n \n\n ",  
" ^---- HERE"  
],  
"script": " ...",  
"lang": "painless",  
"position": {  
"offset": 987,  
"start": 957,  
"end": 1047  
}  
}  
],  
"type": "script\_exception",  
"reason": "runtime error",  
"script\_stack": [  
"errorMessage = /[\"]/.matcher(errorMessage).replaceAll('\'');\n \n\n ",  
" ^---- HERE"  
],  
"script": " ...",  
"lang": "painless",  
"position": {  
"offset": 987,  
"start": 957,  
"end": 1047  
},  
"caused\_by": {  
"type": "null\_pointer\_exception",  
"reason": "Cannot invoke "java.lang.CharSequence.length()" because "this.wrapped" is null"  
}  
}  
},  
"actions":   
},  
"messages": [  
"failed to execute watch transform"  
]  
}

Can someone help me to solve this problem? I only need to find all " characters and replace them using ' characters using painless code inside a watcher (the errorMessage field is a normal String field).

Many thanks!
