Announcements
This site is in read only until July 22 as we migrate to a new platform; refer to this community post for more details.
Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Replace all the instances of a character with replaceAll

Hi,

I want to replace all occurrence of string - "[null]" with "[]"

I tried using replaceAll, but it does't work, as it has issues with treating this a regex instead of string. So it replaces all occurence of - "n", "u", "l" with [].  I tried '\[null\]' as well, even that didn't work

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-ModifiedJsonResponse">
  <DisplayName>AM-ModifiedJsonResponse</DisplayName>
  <Properties/>
  <AssignVariable>
    <Name>NullArray</Name>
    <Value>[null]</Value>
  </AssignVariable>
  <Set>
    <Payload contentType="application/json">
            {replaceAll(payload,NullArray,'[]')}
        </Payload>
  </Set>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
  <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>
​

 

cc - @dchiesa1 @kurtkanaskie 

Solved Solved
0 3 339
2 ACCEPTED SOLUTIONS

try this

<AssignMessage name='AM-Assign-Variable-ReplaceAll'>
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>

  <AssignVariable>
    <Name>NullArray</Name>
    <Value>\u005bnull\u005d</Value>
  </AssignVariable>
  <AssignVariable>
    <Name>replacement</Name>
    <Value>[]</Value>
  </AssignVariable>

  <Set>
    <Payload contentType="application/json">{replaceAll(request.content,NullArray,replacement)}
</Payload>
  </Set>

</AssignMessage>

It uses Unicode escape sequences to denote the open and close square bracket.  

View solution in original post

Darn @dchiesa1 you're quick!

This worked for me

 

<AssignMessage continueOnError="false" enabled="true" name="AM-replace-nulls">
    <AssignVariable>
        <Name>regex</Name>
        <Value>\[null]</Value>
    </AssignVariable>
    <AssignVariable>
        <Name>with</Name>
        <Value>[]</Value>
    </AssignVariable>
    <Set>
        <Payload contentType="application/json">{replaceAll(response.content,regex,with)}</Payload>
    </Set>
</AssignMessage>

 

Pro tip: I asked Gemini for: regex to to match exactly "[null]"

I also have learned to always use a variable for the regex expression.

View solution in original post

3 REPLIES 3

try this

<AssignMessage name='AM-Assign-Variable-ReplaceAll'>
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>

  <AssignVariable>
    <Name>NullArray</Name>
    <Value>\u005bnull\u005d</Value>
  </AssignVariable>
  <AssignVariable>
    <Name>replacement</Name>
    <Value>[]</Value>
  </AssignVariable>

  <Set>
    <Payload contentType="application/json">{replaceAll(request.content,NullArray,replacement)}
</Payload>
  </Set>

</AssignMessage>

It uses Unicode escape sequences to denote the open and close square bracket.  

You are a life saver. Thanks !!

Darn @dchiesa1 you're quick!

This worked for me

 

<AssignMessage continueOnError="false" enabled="true" name="AM-replace-nulls">
    <AssignVariable>
        <Name>regex</Name>
        <Value>\[null]</Value>
    </AssignVariable>
    <AssignVariable>
        <Name>with</Name>
        <Value>[]</Value>
    </AssignVariable>
    <Set>
        <Payload contentType="application/json">{replaceAll(response.content,regex,with)}</Payload>
    </Set>
</AssignMessage>

 

Pro tip: I asked Gemini for: regex to to match exactly "[null]"

I also have learned to always use a variable for the regex expression.