This functionality is available in a Stedi module. Contact us for details.
- A set of mapping expressions, describing how to transform input fields to output fields
- A mapping type, specifying how output fields are determined
- A target example, providing default values for output fields
- A set of schemas, describing what the input and output JSON should look like
- Lookup tables, to make converting values in mapping expressions easier
Mapping expressions
A mapping expression is a piece of code that specifies how to turn input fields into an output field. The following example shows how you could output a total price from a quantity and a unit price.Refer to Write Mapping Expressions for full
details, examples, and JSONata resources.
Mapping types
All mapping types produce output fields based on the mapping expressions. 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.Only mapped keys
By default, Mappings adds the target keys you selected to the output and nothing else. The mappings API handles output fields in the following ways:- Adds an output field for every mapping expression that returns a result
- Does not add output fields based on the target example
- Does not add output fields based on the input.
- The field
currency
is added to the output because there is a mapping expression forcurrency
that produces a result. - The field
total
is not added to the output. There is a mapping expression fortotal
, but it doesn’t produce a result becauseitem.quantity
is missing from the input. - The fields
item.unit_price
anditem.currency
are not added to the output, because there are no mapping expressions for them.
Merge with target example
If you need to produce JSON based on a template, and you only need to change a handful of fields, it’s easier if you copy the entire target to the output and only make those few changes. For example, you may want to use a default currency for your output. The mappings API handles output fields in the following ways:- Adds an output field for every mapping expression that returns a result
- Copies an output field from the target example, unless that output field was already added because of a mapping expression.
- Does not add output fields based on the input.
- The field
currency
is copied from the target example because there is no mapping expression forcurrency
. - The field
unit_price
is not copied from the target example, because there is a mapping expression forunit_price
that produces a result. - The field
quantity
is copied from the target example. There is a mapping expression forquantity
, but that one doesn’t produce a result, becauseitem.quantity
is missing from the input. - The field
total
doesn’t end up in the output. There’s a mapping expression fortotal
, but it doesn’t produce a result and the target example doesn’t contain a default value fortotal
.
Pass through
If your output needs to be much like your input, but with a few values changed, then you can tell Mappings to start with a copy of your input. The mappings API handles output fields in the following ways:- An output field will be added for every mapping expression that returns a result.
- An output field will be copied from the input, unless that output field was already added because of a mapping expression.
- No output fields will be added based on the target example.
- All fields are copied from the input.
- The field
tax_rate
is overwritten because there is a mapping expressions fortax-rate
that produces a result. - The field
item.quantity
is not overwritten. There is a mapping expressions foritem.quantity
, but it doesn’t produce a result, becauseitems_per_unit
is missing from the input. - The field
total
is added, because there’s a mapping expression fortotal
that produces a result.
Target example
The target example is a JSON object that provides default values in case a mapping expression doesn’t return a result. For example, the following target example sets the default currency toUSD
.
Schemas
The Mappings UI uses the source and target schemas are used by the Mappings UI. If you don’t plan on using the Mappings UI—i.e. you’ll use the API exclusively—then you generally don’t need to provide schemas. The only exception is when you want to provide default values for your output fields. Any schema you specify must be a valid JSON Schema (version 2020-12). Also, each schema needs to be self-contained, so you can’t include references to external schemas.Source schema
The Mappings UI uses the source schema to show an example of an input document. You can use this example to find and select the input fields you need in your mapping expressions. The following schema specifies three input fields:quantity
, unit_price
and currency
.
Target schema
The Mappings UI uses the target schema to show an example of an output document. You can only create mapping expressions for fields that are included in the target schema, so without a target schema, you can’t use the Mappings UI. The following schema specifies two output fields:total
and currency
.
USD
.
Lookup tables
A lookup table helps the Mappings API convert a value from one representation to another. For example, the following table allows you to translate country names.english | spanish | german |
---|---|---|
United States | Estados Unidos | Vereinigte Staaten |
Mexico | México | Mexiko |
Germany | Alemania | Deutschland |
$lookupTable
function. The following example uses the lookup table called countries
to translate the input field land
(which is German for country) from German to English.
countries
lookup table in JSON.