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

如何在AWS Step Functions的JSON键中解析JSONata表达式?

问题:AWS Step Functions中JSON键的JSONata表达式未被解析

我尝试在AWS Step Functions的JSON键中解析JSONata表达式,配置如下:

{
  "ApplicationId": "ID",
  "SendUsersMessageRequest": {
    "MessageConfiguration": {
      "GcmMessage": {
        "Action": "OPEN_APP",
        "Title": "Order completed!",
        "SilentPush": false,
        "Sound": "default",
        "Body": "Your order is completed successfully.",
        "Data": {
          "orderId": "{% $order.id.S %}",
          "status": "COMPLETED"
        }
      }
    },
    "Users": {
      "{% $order.userId.S %}": {}
    }
  }
}

但其中的{% $order.userId.S %}表达式未被解析为实际值,而是以字符串形式保留,如何修改才能让键中显示实际值?

Order数据结构:

{
  "order": {
    "id": {
      "S": "d7ea32bf-5dac-45a8-b924-b7752e7d8b09"
    },
    "userId": {
      "S": "11fb0540-20d1-707a-ac85-dcfbe06bad57"
    },
    "status": {
      "S": "COMPLETED"
    }
  }
}

期望输出:

{
  "ApplicationId": "ID",
  "SendUsersMessageRequest": {
    "MessageConfiguration": {
      "GcmMessage": {
        "Action": "OPEN_APP",
        "Title": "Order completed!",
        "SilentPush": false,
        "Sound": "default",
        "Body": "Your order is completed successfully.",
        "Data": {
          "orderId": "d7ea32bf-5dac-45a8-b924-b7752e7d8b09",
          "status": "COMPLETED"
        }
      }
    },
    "Users": {
      "11fb0540-20d1-707a-ac85-dcfbe06bad57": {}
    }
  }
}

解决方案

在AWS Step Functions中,直接在JSON键位置使用{% ... %}插值语法不被支持。要动态生成键名,需要使用JSONata的对象构造器语法,通过$object()函数实现:

修改后的配置如下:

{
  "ApplicationId": "ID",
  "SendUsersMessageRequest": {
    "MessageConfiguration": {
      "GcmMessage": {
        "Action": "OPEN_APP",
        "Title": "Order completed!",
        "SilentPush": false,
        "Sound": "default",
        "Body": "Your order is completed successfully.",
        "Data": {
          "orderId": $order.id.S,
          "status": "COMPLETED"
        }
      }
    },
    "Users": $object([[$order.userId.S, {}]])
  }
}

关键说明:

  • Data中的orderId:直接移除引号和插值语法,使用$order.id.S即可,Step Functions的JSONata支持直接引用路径值。
  • Users的动态键:使用$object()函数,传入包含键值对的数组[[$order.userId.S, {}]],这样会将$order.userId.S解析后的字符串作为键名,对应值为{}。

使用该配置处理给定的Order数据后,会生成你期望的输出结果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 07:47:20