You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在DataWeave中不使用列名创建Excel文件的键值映射

Solution Using DataWeave 2.0

To dynamically map Excel rows to objects using column indices (without hardcoding header names) and add validation errors, follow these steps:

1. Understand the Data Structure

When you read an Excel file into MuleSoft, each row is an object where keys are the Excel column headers, and values are the cell contents. For example:

{"Column A": "valueA", "Column B": "valueB"}

To access columns by index, convert this object into an ordered array of key-value pairs using entriesOf(item). This gives you:

[{"key": "Column A", "value": "valueA"}, {"key": "Column B", "value": "valueB"}]

The order of entries matches the column order in your Excel file (A first, B second, etc.).

2. Dynamic Mapping + Column-Index Validation

Use entriesOf to iterate over columns by index, perform validation, then reconstruct the final object. Here's a complete example:

%dw 2.0
output application/json

// Helper function to validate columns by index
fun validateColumn(index: Number, header: String, value: Any): String? = 
    index match {
        case 0 -> // Validate Column A (index 0)
            if (value is String and value != "") null else "$header cannot be empty"
        case 1 -> // Validate Column B (index 1)
            if (value is Number) null else "$header must be a number"
        // Add more cases for additional columns as needed
        else -> null
    }
---
payload map (row, rowIndex) -> 
    do {
        // Convert row object to ordered key-value entries
        var columnEntries = entriesOf(row)
        
        // Run validation on each column by index
        var validationErrors = columnEntries 
            map ((entry, colIndex) -> validateColumn(colIndex, entry.key, entry.value))
            filter ($ != null) // Keep only error messages
        
        // Format error string (empty if no errors)
        var errorMessage = if (sizeOf(validationErrors) > 0) joinBy(validationErrors, ", ") else ""
        
        // Reconstruct the row object + add Errors field
        ---
        fromEntries(columnEntries) ++ { Errors: errorMessage }
    }

3. Key Details

  • entriesOf(row): Converts the row object into an ordered array of key-value pairs, letting you access columns by their Excel index (0 for A, 1 for B, etc.).
  • fromEntries(columnEntries): Converts the array of key-value pairs back into an object (this replaces your hardcoded key-value mappings dynamically).
  • Validation Logic: The validateColumn function uses the column index to apply specific rules. Adjust this function to match your validation needs (e.g., regex checks, length limits, data types).

Why Your pluck $$ Attempt Didn't Work

pluck $$ returns an array of the object's keys, but pluck itself returns an array of values—not the key-value pairs you need to build the map. entriesOf is the correct tool here because it preserves both the key (header name) and value for each column.

Example Output

For a row where Column A is empty and Column B is a string:

{"Column A": "", "Column B": "not-a-number", "Errors": "Column A cannot be empty, Column B must be a number"}

内容的提问来源于stack exchange,提问作者anxiousAvocado

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 10:57:43