Skip to main content
Inspiring
February 13, 2024
Question

How to filter an array and sum the total

  • February 13, 2024
  • 1 reply
  • 534 views

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 has been closed for replies.

1 reply

Raymond Camden
Community Manager
Community Manager
February 14, 2024

Like so:

 

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

 

Inspiring
February 16, 2024

Thank you