How do I remove this from message which I could only describe as html names like & or <
Can I do it without using replace function ?
Sample message:
{
"message": "Hello world & welcome"
}
Expected output:
{
"message": "Hello world & welcome"
}
If you are lucky those are XML entities. There are 5 XML entities vs hundreds of HTML entities.
There is no built-in function in DataWeave at this time to resolve XML or HTML entities. I wrote a pure DataWeave solution using replace()
where the entities are not fixed in the code, giving flexibility to modify the list of entities as needed. This approach also works for any arbitrary list of strings that needs to be replaced. A little trick is to use reduce()
to apply replace()
for each entity instead of using to accumulate values.
Also I used the update operator to update a specific key of the message.
Script:
%dw 2.0
output application/json
var xmlEntities={
"&": "&",
"<": "<",
">": ">",
"'": "'",
""": "\""
}
fun replaceEntities(s: String, entities)=
entriesOf(entities) reduce ((item, acc = s) -> acc replace item.key with(item.value))
---
payload update {
case m at .message -> replaceEntities(m, xmlEntities)
}
Input:
{
"message": "Hello world & welcome"
}
Output:
{
"message": "Hello world & welcome"
}