Mapping from an EDIFACT JToken Tree
The most common way to map EDIFACT messages is to first convert the source message to JToken and then map the message to the target format because JToken is easy and the most versatile way to process data with Frends. If the target format is XML then you can consider converting the source message first to XML and then use XSLT to create the target XML format. The XML target format can also be created from the JToken format. It is a matter of taste or the organization's preferences regarding the kind of mapping that should be used.
Mapping with a Template Technique
Using a Frends Code Element ("Create Expression" in the Frends Process Editor Toolbox) is the best method for simple mappings where complex mapping logic is not needed.
Currently, the Code Element does not support iterating through repetitive data items like order lines. However, we can split the mapping into steps and first map the repetitive data and then map the header part of the message and place the already mapped repetitive parts to the target message. The next example demonstrates this mapping technique.
In this example, we have already converted the incoming EDIFACT ORDERS message to a JToken object. Next, we will loop over the order rows with a Foreach loop. Note that it is easy to refer to the order rows of the source message with the dot notation path #var.EdifactMessage.Edifact.TSORDERS.LINLoop.Loop_LIN_ORDERS
Inside the loop, each line is mapped to the target JSON format using the Code Element. The expression contains the target template. In other words, the expression contains the static target structure where we dynamically populate the field values from the source data by referring to the source order row fields:
On runtime this result target JSON row will be created:
β
The whole loop's result #result[For each Order Row (LINLoop)] contains an array of target JSON strings.
Next, we will map the header part of the message and add the already mapped orders rows to the target message:
This final mapping is very similar to the mapping part where order rows were mapped, but let's have a closer look at the more complex parts.
β
OrderDate
In the source EDIFACT message, there are two DTM.DATETIMEPERIOD_01 fields containing different dates. Only the DTM.DATETIMEPERIOD_01.Datetimeperiodqualifier_01 field's content tells us whether the date is order date (Datetimeperiodqualifier_01 = '4') or delivery date (Datetimeperiodqualifier_01 = '2').
In this example, we use the JSON Path to select the value of DTM.DATETIMEPERIOD_01.Datetimeperiod_02 where the DTM.DATETIMEPERIOD_01.Datetimeperiodqualifier_01 is equal to '4' to select the order date (date time period qualifier '4' means order date).
To learn more about the JSON Path, please visit Stefan Goessner's JSON Path page.
If you need to test the JSON Path queries, you can use, for example, this online JSON Query tool. Select JSONPath (Goessner 0.8.0) to use the same JSON Path implementation that is used by Frends.
OrderLines
In the earlier loop, the order rows are mapped one by one to the target JSON format. The loop then returns an array of JSON-formatted strings.
To include the order rows in the target message, we need to join the array of JSON strings to be the value of "OrderLines".
This is done with a simple C# expression where we join the strings from the array to one string and have a comma and linefeed between each string from the array:
String.Join(",\n", #result[For each Order Row (LINLoop)])
Resulting JSON target message:
{ "Order": { "MessageReferenceNumber": "000000101", "PaymentMeansCoded": "42", "OrderDate": "20220705", "OrderLines": [ { "ItemNumber": "6413600001584", "ItemDescription": "Frends IPA 0,33L", "Quantity": 500 }, { "ItemNumber": "6413600000280", "ItemDescription": "Frends With Benefits 0,33L", "Quantity": 750 } ] } }
βNote: In addition to querying source JSON fields with the JSON Path, you can query source data in other ways e.g. using standard C# LINQ (Language Integrated Query).
The next article is an Example of Parsing EDIFACT with Edifact ConvertToXml