简体   繁体   English

如何对json dataweave中的值求和

[英]How to sum value in json dataweave

(Mule soft dataweave 1.0) (骡子软dataweave 1.0)

I want to sum second level object in json together.我想将 json 中的二级对象相加。 If I have this json value如果我有这个 json 值

{
    "type": Type1,
    "date" :{
        "day1" : 1,
        "day2" : 2,
        "day3" : 3
    },
    "a" : test
},
{
    "type": Type2,
    "date" :{
        "day1" : 4,
        "day2" : 5,
        "day3" : 6
    },
    "a" : test2
}

How to sum value in date so that the output (totalDate) be like :如何对日期中的值求和,以便输出 (totalDate) 如下:

{
"type": Type1,
"totalDate" : 6,
"date" :{
      "day1" : 1,
      "day2" : 2,
      "day3" : 3
  },
  "a" : test
},
{
  "type": Type2,
  "totalDate" : 15,
  "date" :{
      "day1" : 4,
      "day2" : 5,
      "day3" : 6
  },
  "a" : test2
}

Map , pluck , sum (mule 4), reduce (mule 3): Map , pluck , sum (mule 4), reduce (mule 3):

Assuming your input is an array like this:假设您的输入是这样的数组:

[
    {
        "type": "Type1",
        "date" :{
            "day1" : 1,
            "day2" : 2,
            "day3" : 3
        },
        "a" : "test"
    },
    {
        "type": "Type2",
        "date" :{
            "day1" : 4,
            "day2" : 5,
            "day3" : 6
        },
        "a" : "test2"
    }
]

EDIT:编辑:

In dataweave 1.0, I'd do it this way:在 dataweave 1.0 中,我会这样做:

As a note: The input line IS VALID even in mule apps.注意:即使在 mule 应用程序中,输入行也是有效的。 Editing this away as a mistake is incorrect, even if it's typically unnecessary.将其作为错误进行编辑是不正确的,即使通常没有必要。 Just want to avoid some misinformation from that edit, since there are perfectly valid reasons to define your input at times.只是想避免该编辑中的一些错误信息,因为有时有完全有效的理由来定义您的输入。 For all I know, he is doing a file read and hadn't set the mime type.据我所知,他正在读取文件并且没有设置 mime 类型。 By leaving that line in, it will still process correctly.通过保留该行,它仍将正确处理。

The input directive is not required in some scenarios when the DataWeave execution itself is contextualized and the input derived from said context.当 DataWeave 执行本身被上下文化并且输入从所述上下文派生时,在某些情况下不需要输入指令。 For example, in Mule their event concept provides the context, adding inputs such as payload and vars, with their respective MIME type information.例如,在 Mule 中,他们的事件概念提供了上下文,添加了负载和变量等输入,以及它们各自的 MIME 类型信息。 After this section, we will avoid the input directive and assume a single input named payload throughout the rest of the tutorial.在本节之后,我们将避免使用 input 指令,并在本教程的其余部分中假设一个名为 payload 的输入。

More information here:更多信息在这里:

https://docs.mulesoft.com/mule-runtime/3.9/dataweave-reference-documentation#document-structure https://docs.mulesoft.com/mule-runtime/3.9/dataweave-reference-documentation#document-structure

https://docs.mulesoft.com/mule-runtime/3.9/dataweave-reference-documentation#input-directive https://docs.mulesoft.com/mule-runtime/3.9/dataweave-reference-documentation#input-directive

%dw 1.0
%input payload application/json
%output application/json
---
payload map {
  ($),
  totalDate: ($.date pluck $) reduce $+$$
}

The only difference is that I'm not relying on sum, since it isn't a function available to me.唯一的区别是我不依赖 sum,因为它不是我可用的函数。 The reduce $+$$ means I'm defaulting my accumulator to the first number in the array, and then adding each number after that to it. reduce $+$$意味着我将累加器默认为数组中的第一个数字,然后将之后的每个数字添加到它。 This is in fact how the 3.9 docs recommend doing it too: https://docs.mulesoft.com/mule-runtime/3.9/dataweave-operators#reduce for a more detailed explanation.这实际上也是 3.9 文档建议这样做的方式: https : //docs.mulesoft.com/mule-runtime/3.9/dataweave-operators#reduce以获得更详细的解释。


We can map over each object.我们可以映射每个对象。 We create a new object, and then using ($) deconstruct the existing object into it.我们创建一个新对象,然后使用($)将现有对象解构到其中。 From there, we create a new field which will populate by plucking all of the values from the date object ( $.date pluck $ ) and then toss that into a sum .从那里,我们创建一个新字段,它将通过从日期对象 ( $.date pluck $ ) 中$.date pluck $所有值来填充,然后将其转换为sum

%dw 2.0
output application/json
---
payload map {
    ($),
    totalDate: sum($.date pluck $)
}

Michael's answers can also be modified as...迈克尔的答案也可以修改为......

%dw 2.0
output application/json
---
payload map ((value, index) -> 
    {
        (value),
        totalDate: sum(valuesOf(value.date))
    }
)

Just another way to do it只是另一种方式来做到这一点

%dw 2.0
output application/json
---
payload map (v0,k0) ->
{
    (v0 ++ (totalDate:(v0.date pluck $) reduce(item,acc) -> item + acc))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM