简体   繁体   中英

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

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?

  • 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM