0

Hello everybody I am passing a Json from javascript to an Apex controller to validate some info. The problem is that the json comes with some values like this:

"Direction": {}

And I want to change them to:

"Direction": null

I trying the:

        wrapperWithInfo.replaceAll('{}', 'null');

but I am getting the next error:

System.StringException: Invalid regex: Illegal repetition

public static List<String> getInformationFromLWC(String wrapperWithInfo, String recordIdPuntoSuministro){
    String newRecordIdPasoMensajeria;
    WrapperGlobal newInstance = new WrapperGlobal();
    wrapperWithInfo.replaceAll('\\{}', 'null');
    System.debug('jsonlimpio -> '+ wrapperWithInfo);

2 Answers 2

3

You should use replace(String target, String replacement) instead of replaceAll(String regExp, String replacement):

  • the first replaces each substring of a string that matches the literal target sequence
  • the latter Replaces each substring of a string that matches the regular expression

Keep in mind that Strings are immutable, so you must assign the result of replace to the variable. Indeed every method that modifies a string (replace, substring, etc...) creates a new one.

wrapperWithInfo = wrapperWithInfo.replace('{}', 'null');

If you want to stick with replaceAll, then you have to escape the curly brace as showed by TSmith

2
  • Hello I tried wht you said but it neither work
    – TechGuy
    Jan 24, 2023 at 14:51
  • @salesforcequestions I updated the answer
    – RubenDG
    Jan 24, 2023 at 14:58
1

You need to escape the curly braces as follows:

.replaceAll('\\{}', 'null');

Such braces are used to group parts of a regex together to express repetition.

1
  • Hello it did not gave me any error but it did not replace the values. I updated the question
    – TechGuy
    Jan 24, 2023 at 14:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .