dataweave 2-如何通过索引内部对象加入2个数组,而不会丢失数据父对象

发布于 2025-01-30 07:26:10 字数 1134 浏览 5 评论 0原文

如何通过索引加入3个数组(lines,pckslip和linesDT),并通过linesDT生成一个数组对象,之后您必须生成一个新的字段“ totalCost”,该字段是在LinesDT Array中添加所有成本元素的“成本”字段,请注意原始对象中的“数字”字段均保留给父母产生的所有新元素。

输入:

{
  "lines":[
    {
       "requestedQuantity":1,
       "pendingQuantity":0
    },
    {
       "requestedQuantity":2,
       "pendingQuantity":0
    }
 ],
 "number":"98",
 "pckSlip":[
    {
       "trackingNumber":"10534",
       "boxesNum":1
    },
    {
       "trackingNumber":"00049",
       "boxesNum":1
    }
 ],
 "linesDt":[
    {
       "number":"5678",
       "cost":7.7
    },
    {
     "number":"1234",
     "cost":7.3
    }
 ]
}

OUPUT:

[
  {
     "number":"5678",
     "cost":7.7,
     "requestedQuantity":1,
     "pendingQuantity":0,
     "trackingNumber":"10534",
     "boxesNum":1,
     "totalCost":15,
     "order":"98"
  },
  {
     "number":"1234",
     "cost":7.3,
     "requestedQuantity":2,
     "pendingQuantity":0,
     "trackingNumber":"00049",
     "boxesNum":1,
     "totalCost":15,
     "order":"98"   
  }
]

注意:我们生成2个新元素,因为它们是在元素数组中“ LinesDT”中发现的索引的总数。

任何帮助将不胜感激。谢谢。

How to join 3 arrays by index (lines, pckSlip and linesDt) and generate an arrays object by linesDt after that you have to generate a new field "totalCost" which is to add the "cost" field of all cost elements in the linesDt array, note the "number" field in the original object is preserved for all new elements spawned from the parent.

Input:

{
  "lines":[
    {
       "requestedQuantity":1,
       "pendingQuantity":0
    },
    {
       "requestedQuantity":2,
       "pendingQuantity":0
    }
 ],
 "number":"98",
 "pckSlip":[
    {
       "trackingNumber":"10534",
       "boxesNum":1
    },
    {
       "trackingNumber":"00049",
       "boxesNum":1
    }
 ],
 "linesDt":[
    {
       "number":"5678",
       "cost":7.7
    },
    {
     "number":"1234",
     "cost":7.3
    }
 ]
}

Ouput:

[
  {
     "number":"5678",
     "cost":7.7,
     "requestedQuantity":1,
     "pendingQuantity":0,
     "trackingNumber":"10534",
     "boxesNum":1,
     "totalCost":15,
     "order":"98"
  },
  {
     "number":"1234",
     "cost":7.3,
     "requestedQuantity":2,
     "pendingQuantity":0,
     "trackingNumber":"00049",
     "boxesNum":1,
     "totalCost":15,
     "order":"98"   
  }
]

NOTE: We generate 2 new elements because they are the total of indices found in "linesDt" within an array of elements.

Any help would be appreciated. Thank you.

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

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

发布评论

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

评论(2

满地尘埃落定 2025-02-06 07:26:10

映射行的每个元素可为您提供在其他数组中使用的索引。 ++运算符可用于将所有对象连接在一起。计算出的字段添加为另一个对象。

%dw 2.0
output application/json
var totalCost = sum(payload.linesDt.*cost)
---
payload.lines map (
    $ 
    ++ payload.pckSlip[$]
    ++ payload.linesDt[$]
    ++ {totalCost: totalCost, order: payload.number}
)

输出:

[
  {
    "requestedQuantity": 1,
    "pendingQuantity": 0,
    "trackingNumber": "10534",
    "boxesNum": 1,
    "number": "5678",
    "cost": 7.7,
    "totalCost": 15.0,
    "order": "98"
  },
  {
    "requestedQuantity": 2,
    "pendingQuantity": 0,
    "trackingNumber": "00049",
    "boxesNum": 1,
    "number": "1234",
    "cost": 7.3,
    "totalCost": 15.0,
    "order": "98"
  }
]

Mapping each element of lines gives you the index to use in the other arrays. The ++ operator can be used to concatenate objects all the objects together. The calculated fields are added just as another object.

%dw 2.0
output application/json
var totalCost = sum(payload.linesDt.*cost)
---
payload.lines map (
    $ 
    ++ payload.pckSlip[$]
    ++ payload.linesDt[$]
    ++ {totalCost: totalCost, order: payload.number}
)

Output:

[
  {
    "requestedQuantity": 1,
    "pendingQuantity": 0,
    "trackingNumber": "10534",
    "boxesNum": 1,
    "number": "5678",
    "cost": 7.7,
    "totalCost": 15.0,
    "order": "98"
  },
  {
    "requestedQuantity": 2,
    "pendingQuantity": 0,
    "trackingNumber": "00049",
    "boxesNum": 1,
    "number": "1234",
    "cost": 7.3,
    "totalCost": 15.0,
    "order": "98"
  }
]
当爱已成负担 2025-02-06 07:26:10

假设每个数组的大小将相同。

脚本

%dw 2.0
output application/json
---
1 to sizeOf(payload.lines) map {
    (payload.linesDt[($)] ++ payload.lines[($)] ++ payload.pckSlip[($)] ++ ("totalCost": sum(payload.linesDt..cost) as String) ++ ("order": payload.number))
}

输出

[
  {
    "number": "5678",
    "cost": 7.7,
    "requestedQuantity": 1,
    "pendingQuantity": 0,
    "trackingNumber": "10534",
    "boxesNum": 1,
    "totalCost": "15",
    "order": "98"
  },
  {
    "number": "1234",
    "cost": 7.3,
    "requestedQuantity": 2,
    "pendingQuantity": 0,
    "trackingNumber": "00049",
    "boxesNum": 1,
    "totalCost": "15",
    "order": "98"
  }
]

Assuming that the size of each of the arrays is going to be the same.

Script

%dw 2.0
output application/json
---
1 to sizeOf(payload.lines) map {
    (payload.linesDt[($)] ++ payload.lines[($)] ++ payload.pckSlip[($)] ++ ("totalCost": sum(payload.linesDt..cost) as String) ++ ("order": payload.number))
}

Output

[
  {
    "number": "5678",
    "cost": 7.7,
    "requestedQuantity": 1,
    "pendingQuantity": 0,
    "trackingNumber": "10534",
    "boxesNum": 1,
    "totalCost": "15",
    "order": "98"
  },
  {
    "number": "1234",
    "cost": 7.3,
    "requestedQuantity": 2,
    "pendingQuantity": 0,
    "trackingNumber": "00049",
    "boxesNum": 1,
    "totalCost": "15",
    "order": "98"
  }
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文