How to convert JSON to PDF in MuleSoft

Blog post description.

12/21/20220 min read

Suppose we have to build an integration to convert data from a JSON format and transform it into a PDF document.

Here is an example;Sample JSON payload;

Expected PDF Document;

We can achieve this in two steps;

  1. Convert JSON to Text

This step is necessary to remove double quotes and curly brackets from the payload.

We can use Dataweave to make the transformation, like this:

%dw 2. output text/plain

---

"Name: "++ (payload.name default "")++ "\nCompany: "++ (payload.company default "")++ "\nCountry: "++(payload.country default "")

The "\n" is used to write the values in new lines. So the transformation will look like this;

  1. Convert Text to PDF

We can use Dataweave again to convert the text to a PDF document, like this:

This will produce the expected PDF document.