迭代数组并为每个元素生成一个新变量

发布于 2025-01-17 15:55:59 字数 1441 浏览 1 评论 0原文

我有一个 JSON 数组存储在一个变量中,如下所示,

{
  "Opportunity": {
    "QuoteLineItems": {
      "QuoteLineItem": {
        "ServiceTypeCode": "CC",
        "PhaseLevel": "1",
        "Quantity": "1",
        "UnitPrice": "2,854.50",
        "InvoicedSinceLast": "0.00",
        "LevelItems": {
          "LevelItem": {
            "PhaseLevelItemNumber": "R072",
            "DocumentNotes": {
              "Note": null
            }
          }
        },
        "Colonies": {
          "ColonyStage": {
            "QuoteColonyLineNumber": "1",
            "StageItems": {
              "StageItem": {
                "StageLineProperty": "CHARGE",
                "StageQuoteItemNumber": "ICM_033",
                "DocumentNotes": {
                  "Note": null
                }
              }
              }
            }
          }
        }
      },
      "QuoteLineItem": {

我需要迭代每个 QuoteLineItem 并为每个项目生成一个新的 json 变量,因此我使用 For every 元素,如下所示

在此处输入图像描述

内部对于每个我来说有一个转换 Messgae 生成一个新的 json,如下所示

%dw 2.0
output application/json
---
{
   "productCode" : //Not sure how to access the ServiceTypeCode on the QuoteLineItem 
   "axSequenceNumber" : vars.counter,
}

如何获取 foreach 循环内转换消息中每个 QuoteLineItem 的 ServiceTypeCode。我是 Mulesoft 的新手,非常感谢任何帮助

I have a JSON array stored in a variable which looks like below

{
  "Opportunity": {
    "QuoteLineItems": {
      "QuoteLineItem": {
        "ServiceTypeCode": "CC",
        "PhaseLevel": "1",
        "Quantity": "1",
        "UnitPrice": "2,854.50",
        "InvoicedSinceLast": "0.00",
        "LevelItems": {
          "LevelItem": {
            "PhaseLevelItemNumber": "R072",
            "DocumentNotes": {
              "Note": null
            }
          }
        },
        "Colonies": {
          "ColonyStage": {
            "QuoteColonyLineNumber": "1",
            "StageItems": {
              "StageItem": {
                "StageLineProperty": "CHARGE",
                "StageQuoteItemNumber": "ICM_033",
                "DocumentNotes": {
                  "Note": null
                }
              }
              }
            }
          }
        }
      },
      "QuoteLineItem": {

I need to iterate through each of the QuoteLineItem and generate a new json variable for each Item so I am using the For each element like below

enter image description here

Inside the For each I am having a transform Messgae to generate a new json like below

%dw 2.0
output application/json
---
{
   "productCode" : //Not sure how to access the ServiceTypeCode on the QuoteLineItem 
   "axSequenceNumber" : vars.counter,
}

How do I get the ServiceTypeCode for each QuoteLineItem in the transform message inside the for each loop. I a new to Mulesoft and any help is greatly appreciated

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

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

发布评论

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

评论(1

醉梦枕江山 2025-01-24 15:55:59

quientelineItem是一个对象,而不是数组,因此您不能使用它来迭代。相反,您可以使用 multi-value selector 将所有匹配的钥匙取回数组并将其用于迭代:

  <foreach doc:name="For Each" collection="#[payload.Opportunity.QuoteLineItems.*QuoteLineItem]">

在foreach内部,您会收到输入数组的每个元素作为有效载荷:

%dw 2.0
output application/json
---
{
   "productCode" : payload.ServiceTypeCode
   "axSequenceNumber" : vars.counter
}

我不确定通过将其存储到变量中,您不确定您试图准确地实现的目标。您应该考虑单个转换是否可能无法达到相同的结果。

QuoteLineItem is an object, not an array, so you can't use it to iterate. Instead you can use the multi-value selector to retrieve all matching keys as an array and use that to iterate:

  <foreach doc:name="For Each" collection="#[payload.Opportunity.QuoteLineItems.*QuoteLineItem]">

Inside the foreach you receive each element of the input array as the payload:

%dw 2.0
output application/json
---
{
   "productCode" : payload.ServiceTypeCode
   "axSequenceNumber" : vars.counter
}

I'm not sure what you are trying to achieve exactly by storing that into a variable. You should consider if a single transform may not achieve the same results.

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