%dw 2.0
var baseUrl = "http://alainn-cosmetics.cloudhub.io/api/v1.0"
var urlPage = "http://alainn-cosmetics.cloudhub.io/api/v1.0/items"
var fullUrl = "http://alainn-cosmetics.cloudhub.io/api/v1.0/items"
var pageIndex = 0
var requestedPageSize = 4
output application/json
---
using (pageSize = payload.getItemsResponse.PageInfo.pageSize) {
links: [
{
href: fullUrl,
rel : "self"
},
{
href: urlPage ++ "?pageIndex=" ++ (pageIndex + pageSize) ++ "&pageSize=" ++ requestedPageSize,
rel: "next"
},
({
href: urlPage ++ "?pageIndex=" ++ (pageIndex - pageSize) ++ "&pageSize=" ++ requestedPageSize,
rel: "prev"
}) if(pageIndex > 0)
],
collection: {
size: pageSize,
items: payload.getItemsResponse.*Item map (item) -> {
id: item.id,
'type': item.'type',
name: item.name,
(summary: item.summary) if(item.summary?),
(brand: item.brand) if(item.brand?),
links: (item.images.*image map (image) -> {
href: trim(image),
rel: image.@'type'
}) + {
href: baseUrl ++ "/" ++ item.id,
rel: "self"
}
}
}
}
Use Constant Directives
This DataWeave example builds URLs from constants that are defined as variables (var
) in the DataWeave header. It also conditionally outputs fields if they are present in the input.
Before you begin, note that 2.x versions of DataWeave are used by Mule 4 apps. For
DataWeave in Mule 3 apps, refer to
DataWeave version 1.2 examples.
For other DataWeave versions, you can use the version selector in the DataWeave table of contents.
The example uses these functions:
-
++
(concatenate) to build constant directives into URLs. -
map
to go through the fields in the input. -
if
to outputsummary
andbrand
fields.
DataWeave Script:
Input XML Payload:
<ns0:getItemsResponse xmlns:ns0="http://www.alainn.com/SOA/message/1.0">
<ns0:PageInfo>
<pageIndex>0</pageIndex>
<pageSize>5</pageSize>
</ns0:PageInfo>
<ns1:Item xmlns:ns1="http://www.alainn.com/SOA/model/1.0">
<id>B0015BYNRO</id>
<type>Oils</type>
<name>Now Foods LANOLIN PURE</name>
<images>
<image type="SwatchImage">http://ecx.images-amazon.com/images/I/11Qoe774Q4L._SL30_.jpg
</image>
</images>
</ns1:Item>
<ns1:Item xmlns:ns1="http://www.alainn.com/SOA/model/1.0">
<id>B002K8AD02</id>
<type>Bubble Bath</type>
<name>Deep Steep Honey Bubble Bath</name>
<summary>Disclaimer: This website is for informational purposes only.
Always check the actual product label in your possession for the most
accurate ingredient information due to product changes or upgrades
that may not yet be reflected on our web site. These statements made
in this website have not been evaluated by the Food and Drug
Administration. The products offered are not intended to diagnose,
treat
</summary>
<images>
<image type="SwatchImage">http://ecx.images-amazon.com/images/I/216ytnMOeXL._SL30_.jpg
</image>
</images>
</ns1:Item>
<ns1:Item xmlns:ns1="http://www.alainn.com/SOA/model/1.0">
<id>B000I206JK</id>
<type>Oils</type>
<name>Now Foods Castor Oil</name>
<summary>One of the finest natural skin emollients available</summary>
<images>
<image type="SwatchImage">http://ecx.images-amazon.com/images/I/21Yz8q-yQoL._SL30_.jpg
</image>
</images>
</ns1:Item>
<ns1:Item xmlns:ns1="http://www.alainn.com/SOA/model/1.0">
<id>B003Y5XF2S</id>
<type>Chemical Hair Dyes</type>
<name>Manic Panic Semi-Permanent Color Cream</name>
<summary>Ready to use, no mixing required</summary>
<images>
<image type="SwatchImage">http://ecx.images-amazon.com/images/I/51A2FuX27dL._SL30_.jpg
</image>
</images>
</ns1:Item>
<ns1:Item xmlns:ns1="http://www.alainn.com/SOA/model/1.0">
<id>B0016BELU2</id>
<type>Chemical Hair Dyes</type>
<name>Herbatint Herbatint Permanent Chestnut (4n)</name>
<images>
<image type="SwatchImage">http://ecx.images-amazon.com/images/I/21woUiM0BdL._SL30_.jpg
</image>
</images>
</ns1:Item>
</ns0:getItemsResponse>
Output JSON:
{
"links": [
{
"href": "http://alainn-cosmetics.cloudhub.io/api/v1.0/items",
"rel": "self"
},
{
"href": "http://alainn-cosmetics.cloudhub.io/api/v1.0/items?pageIndex=5&pageSize=4",
"rel": "next"
}
],
"collection": {
"size": "5",
"items": [
{
"id": "B0015BYNRO",
"type": "Oils",
"name": "Now Foods LANOLIN PURE",
"links": [
{
"href": "http://ecx.images-amazon.com/images/I/11Qoe774Q4L._SL30_.jpg",
"rel": "SwatchImage"
},
{
"href": "http://alainn-cosmetics.cloudhub.io/api/v1.0/B0015BYNRO",
"rel": "self"
}
]
},
{
"id": "B002K8AD02",
"type": "Bubble Bath",
"name": "Deep Steep Honey Bubble Bath",
"summary": "Disclaimer: This website is for informational purposes only.\n Always check the actual product label in your possession for the most\n accurate ingredient information due to product changes or upgrades\n that may not yet be reflected on our web site. These statements made\n in this website have not been evaluated by the Food and Drug\n Administration. The products offered are not intended to diagnose,\n treat\n ",
"links": [
{
"href": "http://ecx.images-amazon.com/images/I/216ytnMOeXL._SL30_.jpg",
"rel": "SwatchImage"
},
{
"href": "http://alainn-cosmetics.cloudhub.io/api/v1.0/B002K8AD02",
"rel": "self"
}
]
},
{
"id": "B000I206JK",
"type": "Oils",
"name": "Now Foods Castor Oil",
"summary": "One of the finest natural skin emollients available",
"links": [
{
"href": "http://ecx.images-amazon.com/images/I/21Yz8q-yQoL._SL30_.jpg",
"rel": "SwatchImage"
},
{
"href": "http://alainn-cosmetics.cloudhub.io/api/v1.0/B000I206JK",
"rel": "self"
}
]
},
{
"id": "B003Y5XF2S",
"type": "Chemical Hair Dyes",
"name": "Manic Panic Semi-Permanent Color Cream",
"summary": "Ready to use, no mixing required",
"links": [
{
"href": "http://ecx.images-amazon.com/images/I/51A2FuX27dL._SL30_.jpg",
"rel": "SwatchImage"
},
{
"href": "http://alainn-cosmetics.cloudhub.io/api/v1.0/B003Y5XF2S",
"rel": "self"
}
]
},
{
"id": "B0016BELU2",
"type": "Chemical Hair Dyes",
"name": "Herbatint Herbatint Permanent Chestnut (4n)",
"links": [
{
"href": "http://ecx.images-amazon.com/images/I/21woUiM0BdL._SL30_.jpg",
"rel": "SwatchImage"
},
{
"href": "http://alainn-cosmetics.cloudhub.io/api/v1.0/B0016BELU2",
"rel": "self"
}
]
}
]
}
}