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"?>
<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! Go to Solution.
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.
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.