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

从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_id but the getter is getConId(), payload.con_id will try to call getConId()—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).

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', since class is 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_id case: if your payload is a Java object with a con_id field 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.

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'] or payload['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 use payload[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.

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

SyntaxIdeal Scenario
payload.con_idSimple, 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_idLegacy Mule 3 JSON payload processing

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:48:07