### DataWeave: CSV to XML with Order Aggregation Source: https://context7.com/subhash-builds/mulesoft-dataweave-examples/llms.txt Transforms CSV customer data to XML format while joining with inline order data. This example demonstrates reading embedded CSV, custom functions for filtering, XML attribute creation, and calculating totals with null handling. ```DataWeave %dw 2.0 output application/xml var orders = "OrderID,CustomerID,Amount 101,1,250 102,2,180 103,1,320 104,3,150 105,4,null" fun filterOrder(customer)= read(orders, "application/csv") filter $.CustomerID==customer.CustomerID var removedAttributes=["CustomerID", "Amount"] --- Customers: { (payload map ((item, index) -> "Customer" @(CustomerID: item.CustomerID, CustomerName: item.CustomerName, totalAmount: sum(filterOrder(item).Amount) default 0):"Orders":{ ((filterOrder(item)) map { Order @(lineItem: $$ + 1): $ -- removedAttributes ++{ Amount: ($.Amount default 0) } }) })) } ``` -------------------------------- ### Filtering Inventory Status with DataWeave Custom Function Source: https://context7.com/subhash-builds/mulesoft-dataweave-examples/llms.txt Filters inventory items based on their availability status using a reusable custom function. This example demonstrates function parameters with type annotations, object manipulation using `-` and `++` operators, and the `isEmpty` function for filtering out empty results. It processes an array of order items and returns filtered lists. ```DataWeave %dw 2.0 output application/json fun filterStatus(state: String)= payload map ($ - "items" ++{ "items": ($.items filter ($.status == state)).name }) filter (!isEmpty($.items)) --- { "availableItem": filterStatus("available"), "unAvailableItem": filterStatus("out_of_stock") } ``` -------------------------------- ### Grouping Employees by Name Initial and Department from XML with DataWeave Source: https://context7.com/subhash-builds/mulesoft-dataweave-examples/llms.txt Groups XML employee data first by the initial letter of the employee's name and then by their department. This script demonstrates DataWeave's XML selector syntax, nested `groupBy` operations, and dynamic key creation using `mapObject`. It processes XML input and outputs a JSON object. ```DataWeave %dw 2.0 output application/json --- (payload.employees.*emp groupBy ($.name[0])) mapObject ((value, key, index) -> { (key): (value groupBy ($.dept)) mapObject ({ ($$): $.name } ) }) ``` -------------------------------- ### Calculate Factorial with Tail Recursion Source: https://context7.com/subhash-builds/mulesoft-dataweave-examples/llms.txt Implements a tail-recursive function to calculate factorials efficiently. This approach prevents stack overflow errors by using an accumulator pattern for large input numbers. ```dataweave %dw 2.0 output application/json fun facRec(num, facValue)= if(num==1) facValue else facRec(num-1, num * facValue) --- payload map ({ (("factorial of ") ++ ($)): facRec($, 1) }) ``` -------------------------------- ### XML Order Transformation to Flat CSV with DataWeave Source: https://context7.com/subhash-builds/mulesoft-dataweave-examples/llms.txt Transforms hierarchical XML orders into a flat CSV format. This script utilizes XML attribute access (`@id`), nested `map` operations with index correlation, and the `flatten` function to denormalize order items, including calculations for line totals. The output is a CSV string. ```DataWeave %dw 2.0 output application/csv var orders=payload.Orders.*Order --- flatten(orders.Items map ((item1, index1) -> item1.*Item map ({ "OrderId": orders.@id[index1], "CustomerName": orders.Customer.Name[index1], "Location": orders.Customer.Country[index1], "Product": $.Product, "Qty": $.Qty, "Price": $.Price, "LineTotal": (($.Qty default 0) * ($.Price default 0)), "OrderDate": $.OrderDate }))) ``` -------------------------------- ### Aggregate Sales Data by Region Source: https://context7.com/subhash-builds/mulesoft-dataweave-examples/llms.txt Groups order records by region and calculates total sales per region. Demonstrates the use of groupBy, pluck, and sum functions to transform nested objects into summarized arrays. ```dataweave %dw 2.0 output application/json --- { "salesByRegion": (payload.orders groupBy $.region) pluck ({ "region": $$, "total": sum($.amount), "id": $.id }) } ``` -------------------------------- ### Flattening Order Items with DataWeave flatMap Source: https://context7.com/subhash-builds/mulesoft-dataweave-examples/llms.txt Flattens nested order items into individual records using `flatMap` and `map`. It preserves parent order context (orderId, customerName, orderDate) while expanding each item with calculated total price. Expects a JSON structure with orders and their items. ```DataWeave %dw 2.0 output application/json --- payload.orders.items flatMap ((item1, index1) -> item1 map ({ "orderId": payload.orders.orderId[index1], "customerName": payload.orders.customer.name[index1], "item": $.description, "totalPrice": $.qty * $.price, "orderDate": payload.orders.orderDate[index1] })) ``` -------------------------------- ### DataWeave: Group Customer Orders by Category Source: https://context7.com/subhash-builds/mulesoft-dataweave-examples/llms.txt Groups customer orders by customer ID and calculates total amounts per category using groupBy and mapObject. It computes overall amounts and category-specific totals with nested sum operations, filter, and distinctBy. ```DataWeave %dw 2.0 output application/json --- payload groupBy ($.customerId ) mapObject ((value, key) -> { (key): { "overallAmount": sum(value.amount), "categories": { (value map ((customers, index2) ->{ (customers.category): (sum((value filter ($.category == customers.category )).amount) ) } ) distinctBy ($)) } } }) ``` -------------------------------- ### Recursive JSON Flattening with DataWeave Source: https://context7.com/subhash-builds/mulesoft-dataweave-examples/llms.txt Flattens deeply nested JSON objects into dot-notation keys using tail recursion. It traverses nested objects, builds prefixed key paths, and iterates over key-value pairs. Handles JSON input and produces a flattened JSON output. ```DataWeave %dw 2.0 output application/json fun recursionOrder(orderObj, prefix="") = orderObj pluck ((value, key, index) -> if(value is Object) flatten(recursionOrder(value, (prefix ++ key ++ ".")) ) else (prefix ++ key): value) --- recursionOrder(payload) map { (flatten($)) } ``` -------------------------------- ### Convert Roman Numerals to Decimal Source: https://context7.com/subhash-builds/mulesoft-dataweave-examples/llms.txt Parses Roman numeral strings into decimal integers. Uses a lookup map and array reduction to handle standard values and subtractive notation logic. ```dataweave %dw 2.0 output application/json var romanValues= { "I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000 } fun div(x)= (x splitBy "") map (romanValues[$]) var arr= div(payload.romanNumber) --- { "wholeNumber": (arr map (if(($$ < sizeOf(arr)-1) and ($ < arr[$$ + 1])) -$ else $) reduce ($+$$)) } ``` -------------------------------- ### Prepare Null Values for Salesforce Upsert Source: https://context7.com/subhash-builds/mulesoft-dataweave-examples/llms.txt Filters out null or empty fields from an object and collects them into a separate 'fieldsToNull' array. Utilizes the dw::core::Objects module for key extraction. ```dataweave %dw 2.0 output application/json import * from dw::core::Objects --- payload mapObject ((($$): $) if( $ !=null)) ++ { "filedsToNull": keySet(payload mapObject ((($$): $) if($ == null or $ ==""))) } ``` -------------------------------- ### Summarize Salesforce Upsert Results Source: https://context7.com/subhash-builds/mulesoft-dataweave-examples/llms.txt Categorizes batch upsert results into insert, update, or fail counts. Uses conditional logic and groupBy to aggregate status flags from the Salesforce response. ```dataweave %dw 2.0 output application/json var recordList= payload.items.payload fun getStatistics(created, success)= if((created and success)==true) "insert" else if ((created or success)==true) "update" else "fail" --- ((recordList map ({ final: getStatistics($.created, $.success) } )) groupBy ($.final) ) mapObject ( { ($$): sizeOf($) }) ``` -------------------------------- ### Flatten Nested Structures with Reduce Source: https://context7.com/subhash-builds/mulesoft-dataweave-examples/llms.txt Flattens deeply nested JSON structures using nested reduce operations. Includes conditional logic to reorder object keys based on specific field values. ```dataweave %dw 2.0 output application/json --- payload reduce ((item, acc= []) -> acc ++ (item.order.place.name.asisiselm reduce ((subItem, subAcc=[]) ->subAcc ++ [if(item.order.place.name.firstName == "sachin") { "asisslem": subItem.asile, "Lastname": item.order.place.name.lastname } else { "Lastname": item.order.place.name.lastname, "asisslem": subItem.asile } ] ))) ``` -------------------------------- ### DataWeave: Mask Sensitive Fields with dw::util::Values Source: https://context7.com/subhash-builds/mulesoft-dataweave-examples/llms.txt Masks sensitive fields like passwords using the dw::util::Values module's mask function. It replaces all occurrences of a specified field name with a masked value throughout the payload. ```DataWeave %dw 2.0 output application/json import * from dw::util::Values --- payload mask field("password") with "XXXXXX" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.