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

DataWeave 2.0子节点字符串拼接出现语法错误求助

Fixing the Syntax Error with ++ String Concatenation in DataWeave

Hey there, let's break down why that ++ concatenation is throwing an error in your DataWeave code.

Your original script runs smoothly because you're just referencing contact.name directly, but when you try to append "XXX" with ++, DataWeave's strict type checking kicks in—and it's almost certainly upset because contact.name isn't a string (or it's a null value).

What's Causing the Issue?

DataWeave requires both operands of the ++ operator to be strings (or types that can be implicitly converted to strings). If contact.name is null, a number, boolean, or any other non-string type, trying to concatenate it with "XXX" will trigger that syntax error you're seeing at the ++ position.

How to Resolve It

Here are a few straightforward fixes tailored to different scenarios:

1. Convert contact.name to a String First

Use the toString() function to guarantee you're working with a string, even if contact.name is another data type or null:

%dw 2.0
output application/json
---
payload map ( payload01 , indexOfPayload01 ) -> {
    theId: payload01.Id,
    theName: payload01.Name,
    contacts: payload01.Contacts map ( contact , indexOfContact ) -> {
        contactName: toString(contact.name) ++ "XXX"
    }
}

2. Handle Null Values Explicitly

If contact.name might be null, use the default operator to fall back to an empty string before concatenation:

%dw 2.0
output application/json
---
payload map ( payload01 , indexOfPayload01 ) -> {
    theId: payload01.Id,
    theName: payload01.Name,
    contacts: payload01.Contacts map ( contact , indexOfContact ) -> {
        contactName: (contact.name default "") ++ "XXX"
    }
}

This ensures you never attempt to concatenate null with a string, which DataWeave strictly prohibits.

3. Diagnose the Type of contact.name

If you're unsure what data type contact.name is, add a temporary line to output its type—this helps pinpoint the root cause:

%dw 2.0
output application/json
---
payload map ( payload01 , indexOfPayload01 ) -> {
    theId: payload01.Id,
    theName: payload01.Name,
    contacts: payload01.Contacts map ( contact , indexOfContact ) -> {
        contactNameType: typeOf(contact.name), // Temporary line to check type
        contactName: contact.name ++ "XXX"
    }
}

Run this script, check the contactNameType value in your output, and adjust your code based on the actual type you're dealing with.

Wrap-Up

DataWeave's strong typing means you have to be intentional with data types when using operators like ++. By ensuring both sides of the concatenation are strings (or handling nulls properly), you'll eliminate that syntax error quickly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:54:06