Transforming an entire object into Base64 string and vice versa in Mule 4 and DataWeave 2

06 Jan, 2020 | 3 minutes read

In this article, we will explain how to transform an entire object into Base64 string and vice versa using DataWeave 2 in Mule 4.

Base64 is an encoding and decoding process of converting Base64 to binary data to an ASCII (American Standard for Information Interchange) text format and vice versa used by any successful Senior PHP Engineer worldwide. It is used to transfer data over a medium that only supports ASCII formats, such as email messages on Multipurpose Internet Mail Extension (MIME) and Extensible Markup Language (XML) data.

Mule 4 Binaries module contains helper functions for working with binaries. In order to use this module, you must import it to your DataWeave code. You can do this by adding the following line: import * from dw::core::Binaries to the header of your DataWeave script.

ToBase64(Binary): String

toBase64 Mule 4 function transforms a binary value into a Base64 string (Check Figure 1).

Example:

(Figure 1: toBase64 function)

fromBase64(String): Binary

fromBase64 in Mule 4 function transforms Base64 to string into a binary value (Check Figure 2).

Example:

(Figure2: formBase64 function)

The problem appears when we want to transform an entire object to Base64 string. For example, when we want to convert JSON to Base64, the function toBase64() expects arguments of type binary, but we are sending arguments of type Object and that will cause an error (Check Figure 3).

(Figure 3: Generated error)

You can solve this problem by converting the Object to String | Binary, and in order to achieve that we can use DataWeave function write().

Write(Any, String, Object): String | Binary

write() DataWeave function returns a string or binary with the serialized representation of the value in the specified format (MIME type). This function can write in a different format than the input. Note that the data must be validated in that new format or an error will occur.

Parameters

  • valueThe value to write. The value can be of any supported data type.
  • contentTypeA supported format (or MIME type) to write. Default: application/dw.
  • writerProperties – Optional: Sets writer configuration properties. 

We can combine write() and toBase64() function in a single transform message component and transform the entire object into Base64 String | Binary (Refer to Figure 4).

(Figure 4: Transform message)

To transform fromBase64 entire object we should use fromBase64() function and read() function to read the payload as type JSON.

Read(String | Binary, String, Object): Any

read() DataWeave function reads a string or binary and returns parsed content.

This function can be useful if the reader cannot determine the content type by default.

Parameters

  • stringToParse The string or binary to Base64 read.
  • contentType – A supported format (or content type.) Default: application/dw.
  • readerProperties – Optional: Sets reader configuration properties. 

We can combine read() and fromBase64() function in a single transform message component and transform the entire Base64 object to JSON object (Check Figure 5).

(Figure 5: Transform message)

This was an explanation of how to transform an entire object into Base64 string and vice versa object to string in Mule 4 and DataWeave object to string. If you need more information about it, feel free to contact us and we will help you in your DataWeave Base64 challenges!