如何在 Mule 4 中动态设置 JSON 字段名称

发布于 2025-01-10 13:39:19 字数 631 浏览 0 评论 0原文

从API接收到的JSON。

{
  "success": true,
  "timestamp": 1645988822,
  "base": "EUR",
  "date": "2022-02-27",
  "rates": {
    "AED": 4.140586,
    "AFN": 102.662987,
    "ALL": 121.380824,
    "AMD": 538.7856,
    "ANG": 2.016644,
    "AOA": 559.803561
  }
}

我在数据编织表达式中通过这种方式解析JSON,但它给出了错误

%dw 2.0
output application/json
---
{
    "Result":
    {
        "Data":payload.rates.{Currency}        // Currency=AED  
    }
}

所需的JSON输出应该如下

{
    "Result":
    {
        "Data":4.140586
    }
}

JSON received from API.

{
  "success": true,
  "timestamp": 1645988822,
  "base": "EUR",
  "date": "2022-02-27",
  "rates": {
    "AED": 4.140586,
    "AFN": 102.662987,
    "ALL": 121.380824,
    "AMD": 538.7856,
    "ANG": 2.016644,
    "AOA": 559.803561
  }
}

I am parsing JSON through this way in data weave expression but it gives error

%dw 2.0
output application/json
---
{
    "Result":
    {
        "Data":payload.rates.{Currency}        // Currency=AED  
    }
}

Desired JSON output should be as follow

{
    "Result":
    {
        "Data":4.140586
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

内心旳酸楚 2025-01-17 13:39:19

您使用的语法不正确。 payload.rates 是一个对象,因此您只需使用 动态选择器

%dw 2.0
output application/json
var selectedCurrency="AED"
---
{
    "Result":
    {
        "Data": payload.rates[selectedCurrency]
        
    }
}

输出:

{
  "Result": {
    "Data": 4.140586
  }
}

You are using an incorrect syntax. payload.rates is an object so you can just use the dynamic selector:

%dw 2.0
output application/json
var selectedCurrency="AED"
---
{
    "Result":
    {
        "Data": payload.rates[selectedCurrency]
        
    }
}

Output:

{
  "Result": {
    "Data": 4.140586
  }
}
绝不放开 2025-01-17 13:39:19

尝试以下

输入:

{
  "success": true,
  "timestamp": 1645988822,
  "base": "EUR",
  "date": "2022-02-27",
  "rates": {
    "AED": 4.140586,
    "AFN": 102.662987,
    "ALL": 121.380824,
    "AMD": 538.7856,
    "ANG": 2.016644,
    "AOA": 559.803561
  }
}

Dataweave

%dw 2.0
output application/json
---
{
    "Result":
    {
        "Data":payload.rates.AED        
    }
}

输出:

{
  "Result": {
    "Data": 4.140586
  }
}

Try Below

Input:

{
  "success": true,
  "timestamp": 1645988822,
  "base": "EUR",
  "date": "2022-02-27",
  "rates": {
    "AED": 4.140586,
    "AFN": 102.662987,
    "ALL": 121.380824,
    "AMD": 538.7856,
    "ANG": 2.016644,
    "AOA": 559.803561
  }
}

Dataweave

%dw 2.0
output application/json
---
{
    "Result":
    {
        "Data":payload.rates.AED        
    }
}

Output:

{
  "Result": {
    "Data": 4.140586
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文