Forum Discussion

vgautam64's avatar
vgautam64
New Contributor III
2 years ago
Solved

Reduce an array of objects into a single object with the sum of fields as the result

I have an array of the following form:

I want to convert this array of objects into a single object that has the same fields along with Debit_Amount and Credit_Amount fields summed up.

Any help is appreciated!

  • @shashish.jha - welcome to Community!

    It looks like you want to generate dynamic HTML using the JSON document input wihtin a pipeline, correct? One out-of-the-box solution is to use an XML Generator snap. This does make the syntax a little more strict since XML requires open and close tags, but it will work. I’ve attached an example.
    Dynamic HTML_2023_02_27.slp (7.7 KB)

    To try it out, just upload the pipeline, create a triggered task, and call it from a browser.

    If you want something a bit easier to use that supports CSS and HTML Script within the HTML body, you can contact your CSM to request the HTML Generator custom snap.

    Hope this helps!

7 Replies

  • vgautam64  As I can't copy your json, have created my own, you can use it- 

    Data  ==> 

    {
        "carList": [
        {
        "color""purple",
        "type""minivan",
        "debit_amount"50,
        "credit_amount"7
      },
      {
        "color""red",
        "type""station wagon",
        "debit_amount"100,
        "credit_amount"5
      }, {
        "color""black",
        "type""toyota",
        "debit_amount"200,
        "credit_amount"5
      },
      {
        "color""blue",
        "type""Maruti",
        "debit_amount"300,
        "credit_amount"5
      }
    ]
    }
     
    1. mapper  : 
    $carList.map((val,index)=>{"sum_debit_amount": jsonPath($, "carList[*].debit_amount").reduce((cur,acc)=>cur+acc),"sum_credit_amount" : jsonPath($, "carList[*].credit_amount").reduce((cur,acc)=>cur+acc)}.extend(val)).shift()
    • vgautam64's avatar
      vgautam64
      New Contributor III

      Hi Supratim,

      Thanks for the providing a solution. I tried to validate the expression with the data I have and it seems to work when I validate the pipe. Strangely though, I observe that when I execute (not validate) the pipeline, the mapper that holds the expression only seem to process 50 records and gets stuck processing the 51st doc. But when I validate the pipe I am able to see the preview of 148 documents.

      Any idea why this would happen?

  • Hi vgautam64 ,

    Try the following expression in a mapper:

    $group.reduce((acc,curr,ind)=> acc.extend(curr.mapKeys((val,key)=>key+ind)),{})

    Let me know what you think.

    Regards,

    Bojan

  • vgautam64 - I believe this solves your question in the simplest way I can think of.  Use a Mapper snap with the following configuration:

    It appears to me that you only need the field elements and values from the first group element: $group[0]

    The two jsonPath statements are simply pulling all values for the Debit and Credit Amounts into an array of numbers

    Finally, the Array.reduce() method is used to aggregate that list of numbers into a final total.

    Hope this helps!