This functionality is available in a Stedi module. Contact us for details.
- Mapping expression cheatsheet: A collection of more common patterns for mapping expressions
- JSONata documentation: A description of the JSONata language
Basic mapping expressions
You start with a target field and write a mapping expression to select the corresponding source field. The simplest mapping expression points to a single source field. Source
The output will contain the key
phone_number. In this case, the mapping expression is simply the name of a field in the source.
Nested source fields
Use a path to select nested source fields Source
A path contains the key names at every level of a field, separated by dots.
It can be tedious to write out the path for every mapping expression, especially if you have a source with a deeply nested structure. In the Mappings UI, you can click on the field in the source to copy the path to your clipboard.
Lists
To map a list in the source to a list in the target, you need to take two steps.- Write a mapping expression to specify which list you need from the source.
- Write a mapping expression for each field inside the list of your target.
orders. In the mappings UI, the field is marked with the word array, which is another word for list. In the source, the list that contains the relevant data has the path transaction.order.products, so that’s the mapping expression you need for orders.
Specify a mapping expression for each field in orders. For example, the field product_number is called id in the source.
The mapping expressions for the fields in the list don’t include
transaction.order.products, because Mappings knows that those fields are relative to the context of the list. For that reason, the mapping expression for a list is referred to as a list context.
List indexes
If the list entries of your target document are expected to include a list index number, you can access it by binding a positional variable to the List Context.Read about positional variable binding in the JSONata
docs.
index property:
Source
index field. Positional variables in JSONata are zero-based.
Once the expression gets evaluated, you can verify that the
index property is successfully populated for every item in the Output JSON document. This approach could create output like the following example.
Lists with one value
If a list contains only one value, it will show up in the output as a single value instead of as a list. Consider the following example. Source
You’d expect the output to be a list, just like the target example, but because there’s only one product, the result is a single value.
[] at the end of the mapping expression. The following example shows the new output.
Objects
In a generic case, to map an object in the source to an object in the target, you don’t need to do anything on the object level, you only need to define expressions for each field inside of your target object. Sourceorder object.
There is no expression specified on the
order level, so all of its children have to specify a path relative to the root of the source document.
Object context
Object context is an optional expression that can be provided for any field which contains a single JSON object. By providing an object context you can improve your mappings in two ways:- Avoid repetition of the data transformation within child field expressions.
- Omit whole objects from the output based on a condition.
Avoiding repetition within child field expressions
Another way to solve the same mapping from the previous example, would be to provide an object context for theorder, and remove the common part of the path from expressions of its child fields.
Removal of the repeated path prefix was not so dramatic, but imagine you have to map a particular member of an array in the source document, to a single object in the output document.
Source
LV- prefix.
The mapping expressions for the fields in the list don’t include the filter expression, because Mappings knows that those fields are relative to the context of the object. For that reason, the optional mapping expression for an object is referred to as an object context.
Conditionally omitting objects
Mappings allows to skip an object with all of its child fields from the output based on a condition. To achieve that, specify a custom object context that evaluates to an $omitField constant when your desired conditions are met. Let’s consider a case, where the source document may contain an array of products of variable length, and if the count of products in the source is0, a certain object should not be populated in the output.
Source
order object from the output, you should provide a ternary condition as an optional object context, and return $omitField when zero products were found in the source.
When the omitting condition is not met, you can pass down the parent context variable $, which in this case would evaluate to the whole source document, the same as not providing and object context at all.
Advanced mapping expressions
Mappings allows you to do more advanced things than selecting fields. Unless you’re an experienced programmer, writing complex mapping expressions will take some getting used to. We’ll provide some common patterns here. If you’re looking for more, check out our mapping expressions cheatsheet.Text to number
Sometimes, your source will have quotes around a number. When that happens, Mappings thinks it’s dealing with text. You can convert the text to a number by using the$number function.
Source
Number to text
If your target contains a number in quotes, then to Mappings, that’s text instead of a number. You can convert the number to text by using the$string function. String is another word for text.
Source
Calculations
You can do calculations on numbers using*, /, +, and -.
Source
If the numbers in the source are surrounded by quotes, you need to convert them first using the
$number function.
Source
Sum and average
You can calculate the sum and average of a list of numbers using$sum and $average.
Source
Often, the numbers you’re interested in are part of a more complex structure.
Source
In this case, you can refer to the field you’re interested in by its full path (
price). Make sure to put [] after the name of the list (orders) to let Mappings know that you want all prices in the list.
Putting text together
When you have to two text fields and you want to put them together, you can use&.
Source
Taking text apart
You can extract a small part out of a text by using$substring. You need to specify where the part is that you’re interested in, so this only works for text that follows a predictable pattern.
Source
The two numbers let
$substring know where the part begins, and how many letters you want. $substring starts counting characters at 0, so in the example above, we start at the second characters.
If the part you’re interested in is at the back of the text, you can use a negative number to tell $substring to start counting from the last character.
Source
Splitting text
Sometimes a text contains multiple pieces of data, separated by a character. You can turn the text into a list using$split.
Source
You can assign each item in the list to a field by using an index, which is a number between square brackets. Items in a list are numbered starting at 0.
Source
Lookup table
If you have a field that contains a code that you want to replace with a related value, you can build a lookup table. Source- Clicking the edit icon next to the mapping expression.
- Click Lookup tables and select Add new.
- Enter values for your table. You can add values manually, or load them from CSV.
$lookupTable. For example, you might create a lookup table for country codes and then write the following expression.
A lookup table isn’t limited to two values per entry; a row can have as many values as you need. For example, you could create a currency lookup table with two columns:
code for the country code and symbol for the currency symbol.
Source
Lookup table wildcards
You can use Lookup Tables and wildcards for matching multiple possible input options at once. In your lookup table, replace the interchangeable part of the key you want to match against with the* symbol (or any other sequence of symbols, you will be able to select what to match against during the $lookupTable function call).
-> Note: You can replace multiple parts of your key with *.
Any input value that matches the loosely defined wildcard-based lookup table value is now matched when the { "wildcard": "*" } is passed as an optional parameter to the $lookupTable function.
Mapping types
The mapping type specifies how the Mappings API generates the output field when the mapping expression doesn’t produce a value. A mapping expression may not produce a value when one or more of the input fields that the mapping expression depends on aren’t present. You can choose between the following mapping types:- Only mapped keys
- Merge with target example
- Pass through
Omitting output fields
There are times when a field is present in the target, but you don’t want it to end up in the output. You have two options.- Deselect the target field.
- Use the
$omitFieldconstant.
Deselecting target fields
If you don’t provide a mapping expression for a target field, or if a mapping expression doesn’t produce a result, the field may still end up in the output. If you don’t want that, you can deselect the target field. In the following example, none of the fields in thetotals object has a mapping expression associated with it, but it still ends up in the output.
Target
Output
totals to show up at all, select Target keys and deselect the field.
This doesn’t apply when you set the mapping type to Merge with target example, because that option will always copy the values from the the target, unless you use $omitField.
$omitField
Whether a target field should end up in the output is not always a simple yes-or-no question. Sometimes, it depends on the result of the mapping expression. In that case you can use$omitField to tell Mappings when to skip the field.
This is particularly useful if the mapping type is set to Merge with target example and you don’t want to use the default value. In the following example, the total price is included only if the amount of products is larger than 0.
Target
Input
$omitField, the output would’ve included the totalPrice with its default value of 3000, which is clearly wrong in this case.