JSON to EDI Transform – Conditional Segments

Learn how to create an EDI 810 Invoice message from a JSON document using Liquid Data Mapper. We focus on mapping invoice items to the optional SAC segment, avoiding empty segments, and filtering out zero discount values in the output.


Creating a Mapping from JSON to EDI

In this example we will create an EDI 810 Invoice message from a JSON document in Liquid Data Mapper. We will be focusing on the the SAC (Service, Promotion, Allowance, or Charge Information) segment.

In our data source JSON document, each invoice item contains a discount property that can be null, a number (including 0) or because it is optional, may not be present.

So our sample set of data for the discount values looks like this:

    "Items": [
        {
            "quantity": 45,
            "unit_price": 45.99,
            "product": "Widget",
            "discount": null
        },
        {
            "quantity": 5,
            "unit_price": 0.56,
            "product": "Bolt",
            "discount": 12
        },
        {
            "quantity": 6,
            "unit_price": 0.12,
            "product": "Nut"
        },
        {
            "quantity": 90,
            "unit_price": 0.09,
            "product": "Nails",
            "discount": 0
        }
    ]

Now let’s look at the data mapping containing a JSON Reader Component and an EDI Writer Component:

A diagram showing the structure of a JSON Reader in a data mapping tool, displaying connections between invoice items and EDI segments for processing.

We are linking each invoice item declared in the JSON schema to the (IT1, CRC, QTY, CUR, IT3, TX...) Loop of the EDI 810 Invoice, so the data mapper will output entries for each source invoice item.

As we have connected the discount value to the [SAC05] 610 - Amount, this gives us the following EDI:

    IT1**45**45.99~
    PID*F*08***Widget~
    SAC~
    IT1**5**.56~
    PID*F*08***Bolt~
    SAC*****12~
    IT1**6**.12~
    PID*F*08***Nut~
    SAC~
    IT1**90**.09~
    PID*F*08***Nails~
    SAC*****0~

Focussing on the SAC segments, you will notice that there is a SAC segment written out for each item in the source JSON. But what if we only want to write out a SAC segment when we have a discount value in the source JSON?

What is the transform doing?

To rectify this, let’s consider how the transform is executed.

The transform is executed from the target’s (EDI writer) root connection point, it then evaluates all its child connection points recursively.

In simple terms, if you consider a transform as a tree with the root node being the connection point in the output component (EDI writer), then it’s evaluated right to left, top to bottom.

What does Evaluating a connection point mean?

When a connection point is encountered, if it has a connector then this is evaluated, the connector provides a sequence of values by evaluating all of the connection points to the left.

For each value in the sequence returned by the connectors, an entity representing the connection point is written to the output file.

In the case of our SAC segment this means the segment is written into the file:

    SAC~

The child connection points are then evaluated.

But what if the Connection Point has no connector?

If the connection point being evaluated has no connectors, but some of its children (or children’s children etc.) do have connectors, then it is considered to have been passed as sequence with a single value. Because of this, it will always be evaluated (output into the target document).

So, in our case the SAC segment connection point does NOT have a connector, but its child [SAC05] 610 - Amount does. For this reason, the SAC segment is always output.

How do we stop the SAC segment being written out when we have no data to put in it?

If the SAC connation point is passed an empty sequence, then it will not be written to the output file.

If we connect it to the discount value, then we will only get a SAC segment when we have a discount value:

Diagram showing the structure of a JSON document with invoice items and their corresponding properties, including quantity, unit price, and discount.
    IT1**45**45.99~
    PID*F*08***Widget~
    IT1**5**.56~
    PID*F*08***Bolt~
    SAC*****12~
    IT1**6**.12~
    PID*F*08***Nut~
    IT1**90**.09~
    PID*F*08***Nails~
    SAC*****0~

OK, this is much better, we no longer have the empty SAC entries.

However, we still get a SAC segment when the discount value is 0.

In order to remove the 0 value items, we need to filter them out by adding a Filter Component to the data mapping:

A diagram illustrating the mapping process in Liquid Data Mapper, showcasing JSON data input connected to EDI segments, with a focus on filtering 'discount' values for the SAC segment.

We now only get a SAC segment when the discount is a non-zero value:

    IT1**45**45.99~
    PID*F*08***Widget~
    IT1**5**.56~
    PID*F*08***Bolt~
    SAC*****12~
    IT1**6**.12~
    PID*F*08***Nut~
    IT1**90**.09~
    PID*F*08***Nails~

Conclusion

We can easily create an EDI 810 Invoice message from a JSON document using Liquid Data Mapper. By changing the values presented to container nodes we can affect which parts of the tree appear in the output file.

If a connection point has no connector, but it has child nodes that do have connectors, then it is treated as if it has been provided with a single value and is evaluated. However, we can use the Filter Component to explicitly filter the content that is written to the output file.

Download a Free Trial of Liquid Data Mapper

Discover more from Liquid Technologies Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading