Mule DataWeave - XML to json transformation - check XML attribute exists

1.1k Views Asked by At

This is my payload before transformation:

Payload: <stock article_id="28932" lot_number="282779"/>

This is the dataweave code to transform xml to json:

stockPayload: {
        articleId: payload.stock_adjustment.@article_id,
        lotNumber: payload.stock_adjustment.@lot_number,
        uom: payload.stock_adjustment.@uom,
    }

Now the uom is an optional field so it wouldn't be in xml attribute sometime. This transformation is currently throwing mapping error due to uom attribute not found.

How can I allow the dataweave to transform even attribute is not in xml?

1

There are 1 best solutions below

0
On
  • For XML to JSON transformation

, you can try below:

Skip Null On

Whenever the output is of XML or JSON type and has null values in its elements or attributes, you can specify whether this generates an outbound message that contains fields with "null" values, or if these fields are ignored entirely. This can be set through an attribute in the output directive named skipNullOn, which can be set to three different values: elements, attributes, or everywhere.

%output application/xml skipNullOn="everywhere"

When set to:

elements: A key:value pair with a null value is ignored.

attributes: An XML attribute with a null value is skipped.

everywhere: Apply this rule to both elements and attributes.

Ref below link for more details,

DWL reference