|
| 1 | +--- |
| 2 | +title: "Data Transformers" |
| 3 | +url: /refguide/data-transformers/ |
| 4 | +weight: 50 |
| 5 | +description: "Describes Data Transformer documents in Mendix Studio Pro." |
| 6 | +beta: true |
| 7 | +--- |
| 8 | + |
| 9 | +## Introduction |
| 10 | + |
| 11 | +Data Transformer documents transform data from one structure into another structure. In practice, this is a message-to-message transformation within Studio Pro. |
| 12 | + |
| 13 | +You can use this feature to pre-process an incoming message (for example, from an API response or MQTT message) before an import mapping. You can also use it to transform a message before passing it on to a downstream system that expects the data in a specific structure. |
| 14 | + |
| 15 | +{{% alert color="info" %}} |
| 16 | +This feature is in beta. |
| 17 | +{{% /alert %}} |
| 18 | + |
| 19 | +### Prerequisites |
| 20 | + |
| 21 | +* [Studio Pro 11.11](https://marketplace.mendix.com/link/studiopro/11.11.0) or higher |
| 22 | + |
| 23 | +### Limitations |
| 24 | + |
| 25 | +Data transformers currently have the following limitations: |
| 26 | + |
| 27 | +* Only JSON-to-JSON transformation with JSLT (a JSON transformation language) is supported |
| 28 | + |
| 29 | +### Usage Example |
| 30 | + |
| 31 | +Consider an API that returns customer data with many fields, but you only need a few specific fields for your Mendix app: |
| 32 | + |
| 33 | +**Input JSON from API:** |
| 34 | + |
| 35 | +```json |
| 36 | +{ |
| 37 | + "customer_id": "12345", |
| 38 | + "first_name": "John", |
| 39 | + "last_name": "Smith", |
| 40 | + "email": "john.smith@example.com", |
| 41 | + "phone": "+1-555-0123", |
| 42 | + "address": "123 Main St", |
| 43 | + "city": "New York", |
| 44 | + "country": "USA", |
| 45 | + "account_status": "active", |
| 46 | + "credit_limit": 5000, |
| 47 | + "internal_notes": "VIP customer", |
| 48 | + "created_date": "2024-01-15" |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +**JSLT Transformation:** |
| 53 | + |
| 54 | +```jslt |
| 55 | +{ |
| 56 | + "id": .customer_id, |
| 57 | + "fullName": .first_name + " " + .last_name, |
| 58 | + "email": .email, |
| 59 | + "location": .city + ", " + .country |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +**Output JSON:** |
| 64 | + |
| 65 | +```json |
| 66 | +{ |
| 67 | + "id": "12345", |
| 68 | + "fullName": "John Smith", |
| 69 | + "email": "john.smith@example.com", |
| 70 | + "location": "New York, USA" |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +In this example, the data transformer extracts only the required fields, combines the first and last name into a single field, and creates a location string from the city and country. This simplified output can then be easily mapped to your Mendix entities. |
| 75 | + |
| 76 | +## Add the Data Transformer Document |
| 77 | + |
| 78 | +To add a data transformer document to your app, follow these steps: |
| 79 | + |
| 80 | +1. Right-click the module you want to add the data transformer document to. |
| 81 | +1. Select **Add other** > **Data Transformer**. |
| 82 | +1. Name the data transformer: |
| 83 | + |
| 84 | + {{< figure src="/attachments/refguide/modeling/integration/data-transformers/add-data-transformer.png" alt="Add Data Transformer dialog" >}} |
| 85 | + |
| 86 | +1. In the **Input JSON** editor, paste a JSON snippet that you want to transform. |
| 87 | +1. Define the transformation in the **JSLT transformation** editor. |
| 88 | +1. Click **Test Transformation** below the JSLT transformation editor to preview the transformation result in **Output JSON**: |
| 89 | + |
| 90 | + {{< figure src="/attachments/refguide/modeling/integration/data-transformers/define-transformation.png" alt="Data Transformer interface showing Input JSON, JSLT transformation, and Output JSON editors" >}} |
| 91 | + |
| 92 | +## Use the Data Transformer in a Microflow |
| 93 | + |
| 94 | +To perform a transformation in a microflow, complete the following steps: |
| 95 | + |
| 96 | +1. Drag the **Transform JSON** activity into a microflow, preferably after a REST call or anything that provides input for the transformation: |
| 97 | + |
| 98 | + {{< figure src="/attachments/refguide/modeling/integration/data-transformers/transform-json-dialog.png" alt="Add Transform JSON activity" >}} |
| 99 | + |
| 100 | +1. Double-click the activity and click **Select** to choose an existing data transformer document or to create a new one. |
| 101 | +1. Click the **Variable (String)** drop-down and select the input string variable from the list. |
| 102 | +1. Specify the name of the output in the **Variable name** text field. |
| 103 | +1. Click **OK**: |
| 104 | + |
| 105 | + {{< figure src="/attachments/refguide/modeling/integration/data-transformers/configure-transformer-activity.png" alt="Configure Transform JSON activity dialog" >}} |
| 106 | + |
| 107 | +## Data Transformer Output Uses |
| 108 | + |
| 109 | +You can use your transformed JSON snippet in the following ways: |
| 110 | + |
| 111 | +* Create a new JSON structure for import mapping |
| 112 | +* Pass the transformed JSON to downstream systems |
| 113 | +* Use it as input for further processing in a microflow |
| 114 | + |
| 115 | +## Use Cases |
| 116 | + |
| 117 | +You can use Data Transformer documents for the following use cases: |
| 118 | + |
| 119 | +* [Filter out unused fields](/refguide/data-transformer-use-cases/#filtering-out-unused-fields) |
| 120 | +* [Simplify nested structures](/refguide/data-transformer-use-cases/#simplifying-nested-structures) |
| 121 | +* [Normalize objects to arrays (working with dynamic keys)](/refguide/data-transformer-use-cases/#normalizing-objects-to-arrays) |
| 122 | +* [Zip metadata with data](/refguide/data-transformer-use-cases/#zipping-metadata-with-data) |
| 123 | +* [Flatten bill of materials (BOM)](/refguide/data-transformer-use-cases/#flattening-bill-of-materials) |
| 124 | +* [Extract information from a string](/refguide/data-transformer-use-cases/#extracting-information-from-a-string) |
| 125 | +* [Work with SPARQL query results](/refguide/data-transformer-use-cases/#working-with-sparql-query-results) |
| 126 | + |
| 127 | +## Read More |
| 128 | + |
| 129 | +* Mendix resources: |
| 130 | + * For detailed examples of Data Transformers in action, see [Data Transformer Use Cases](/refguide/data-transformer-use-cases/) |
| 131 | +* GitHub resources on JSLT |
| 132 | + * A short [introduction](https://github.com/schibsted/jslt/blob/master/README.md#jslt) and [tutorial](https://github.com/schibsted/jslt/blob/master/tutorial.md#jslt-tutorial) on how to use JSLT: |
| 133 | + * A complete list of [functions available in JSLT](https://github.com/schibsted/jslt/blob/master/functions.md#jslt-functions) |
0 commit comments