简体   繁体   中英

How to sum value in json dataweave

(Mule soft dataweave 1.0)

I want to sum second level object in json together. If I have this json value

{
    "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 :

{
"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):

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:

As a note: The input line IS VALID even in mule apps. 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. 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. For example, in Mule their event concept provides the context, adding inputs such as payload and vars, with their respective MIME type information. After this section, we will avoid the input directive and assume a single input named payload throughout the rest of the tutorial.

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#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. The reduce $+$$ means I'm defaulting my accumulator to the first number in the array, and then adding each number after that to it. 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.


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 .

%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))
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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