从Payload获取属性的方法及不同语法的差异与适用场景咨询
Accessing Payload Properties: Syntax Differences & Use Cases
Let’s break down all the common ways to pull properties from a payload, along with why you might see different results (like your con_id example) and when to use each approach. This is focused on DataWeave (used in MuleSoft), since the syntax you’re using matches that ecosystem.
1. Dot Notation: #[payload.con_id]
This is the most straightforward syntax for accessing properties, but it has strict rules:
- Best for: Simple property names that are valid identifiers (no spaces, special characters, or reserved keywords; start with a letter or underscore).
- Why it might return null:
- If your payload is a Java object instead of a JSON object: DataWeave prioritizes getter methods here. So if your Java class has a field
con_idbut the getter isgetConId(),payload.con_idwill try to callgetConId()—if that method returns null (or doesn’t exist), you’ll get a null result. - The property doesn’t exist in the payload (typo, wrong case, or the payload isn’t an object type at all).
- If your payload is a Java object instead of a JSON object: DataWeave prioritizes getter methods here. So if your Java class has a field
2. Quoted Dot Notation: #[payload.'con_id']
This is a variation of dot notation that adds explicit quoting around the property name:
- Best for:
- Property names that are reserved keywords (like
payload.'class', sinceclassis a Java keyword that can’t be used directly with plain dot notation). - Accessing raw fields on Java objects (bypasses getter methods). This is almost certainly why it works for your
con_idcase: if your payload is a Java object with acon_idfield but no matching getter method, this syntax pulls the field value directly instead of trying to call a getter. - Exact case-sensitive matching when you want to avoid any implicit name resolution logic.
- Property names that are reserved keywords (like
3. Bracket Notation: #[payload['con_id']]
Bracket notation is the most flexible option for property access:
- Best for:
- Property names with special characters (spaces, hyphens, numbers at the start, e.g.,
payload['user-123']orpayload['order total']—these can’t be used with dot notation at all). - Dynamic property access: If you have a variable holding the property name (e.g.,
var propName = 'con_id'), you can usepayload[propName]to fetch the value dynamically—something dot notation can’t handle. - Iterating over object properties or working with dynamic data structures where property names aren’t known upfront.
- Property names with special characters (spaces, hyphens, numbers at the start, e.g.,
4. JSON Selector Notation: #[json:con_id]
This is a legacy syntax from older Mule versions (Mule 3 and earlier):
- Best for: Only maintaining or working with legacy Mule 3 codebases. It’s a simplified way to extract properties from JSON payloads, but it’s been fully replaced by DataWeave’s more powerful syntax in Mule 4+. In modern projects, you should stick to the first three approaches instead.
Quick Cheat Sheet
| Syntax | Ideal Scenario |
|---|---|
payload.con_id | Simple, valid identifier property names on JSON/Java objects (uses getters for Java) |
payload.'con_id' | Java object fields (bypasses getters) or keyword property names |
payload['con_id'] | Special character names, dynamic property access |
json:con_id | Legacy Mule 3 JSON payload processing |
内容的提问来源于stack exchange,提问作者Surendra
相关产品推荐
相关产品推荐

