Skip to main content
Inspiring
Feb 13, 2024 9:04 PM UTC
Question

How to filter an array and sum the total

i have a JSON data as follows:

{

   "transactions": [

     {"city":"toronto", "sales":1234.42, "account":"john"},

     {"city":"toronto","sales":4502.54, "account":"mary"},

     {"city":"montreal","sales":520.04, "account":"tony"}

  ]

}

 

In Word, i create a table to show sales, filtered for "toronto" using the "discard-if" expression. 

 

I've added a table footer that i want to perform a "sum" on "sales" using JSONata's $sum function: Numeric aggregation functions · JSONata

 

The sum fuction operates on all elements of the array instead of the filtered array. Is there a way to dynamically sum on the filtered array? Is this supported?

 

Thanks for any guidance.

 

This topic is closed to new replies. Start a new post to keep the conversation going.

2 replies

Raymond Camden
Community Manager
Community Manager
Feb 14, 2024 6:15 PM UTC

Like so:

 

{{$sum(transactions[city="toronto"].sales)}}

 

Translate
Inspiring
Feb 16, 2024 4:50 PM UTC

Thank you

Translate