How get sum from an array
Status: Closed · Asked by Jithin on · 0 views
I'm working with Square webhooks and I'm sending an invoice with Deposit + Multiple Installment structure. They are all under payment request array. So I'm accepting the webhook data as raw data so that I can work with array instead of single elements. I've stored the raw array to an iterator thinking that I'd be able to SUM the output of the iterator to get the total amount paid etc. But apparently its not working. How would you go about summing up values in an array or otherwise where the number of elements to add up is not fixed and changes depending on the use case.
Hey @Jithin
Please provide the reference of the specific JSON array value you wish to calculate from Square's webhook response, so we can explore the possibilities.
[
{
"automatic_payment_source": "NONE",
"computed_amount_money": {
"amount": 20000,
"currency": "AUD"
},
"due_date": "2024-06-19",
"fixed_amount_requested_money": {
"amount": 20000,
"currency": "AUD"
},
"reminders": [
{
"relative_scheduled_days": -7,
"status": "NOT_APPLICABLE",
"uid": "95d7ec33-1a32-425d-bdeb-8e5f"
},
{
"relative_scheduled_days": 0,
"status": "NOT_APPLICABLE",
"uid": "e9bf2073-425e-4141-8a81-5f0edfc1"
},
{
"relative_scheduled_days": 1,
"status": "NOT_APPLICABLE",
"uid": "c6b9bd38-2b6c-4bda-b8b6928e528d"
},
{
"relative_scheduled_days": 3,
"status": "NOT_APPLICABLE",
"uid": "b44e2604-8a72-4618-9fdc-4be228df"
}
],
"request_type": "DEPOSIT",
"tipping_enabled": false,
"total_completed_amount_money": {
"amount": 20000,
"currency": "AUD"
},
"uid": "0eb4d1cd-42ff-4d25-b286-15de9a5"
},
{
"automatic_payment_source": "NONE",
"computed_amount_money": {
"amount": 136000,
"currency": "AUD"
},
"due_date": "2024-07-17",
"fixed_amount_requested_money": {
"amount": 136000,
"currency": "AUD"
},
"reminders": [
{
"relative_scheduled_days": -7,
"sent_at": "2024-07-10T01:04:05Z",
"status": "SENT",
"uid": "7d8c2428-90a0-4d34-b163-1108b6ee"
},
{
"relative_scheduled_days": 0,
"status": "NOT_APPLICABLE",
"uid": "f42a689c-e27e-4b55-9f47-a0f7fb9ef"
},
{
"relative_scheduled_days": 1,
"status": "NOT_APPLICABLE",
"uid": "f2efdf14-415f-4439-a750-21b9cbf97"
},
{
"relative_scheduled_days": 3,
"status": "NOT_APPLICABLE",
"uid": "5a4da270-930c-473b-bb14-5bc8b1a46"
}
],
"request_type": "INSTALLMENT",
"tipping_enabled": false,
"total_completed_amount_money": {
"amount": 136000,
"currency": "AUD"
},
"uid": "69a88790-3ae6-40d5-b87d-075d14"
},
{
"automatic_payment_source": "NONE",
"computed_amount_money": {
"amount": 86000,
"currency": "AUD"
},
"due_date": "2024-07-18",
"fixed_amount_requested_money": {
"amount": 86000,
"currency": "AUD"
},
"reminders": [
{
"relative_scheduled_days": -7,
"status": "NOT_APPLICABLE",
"uid": "837a7718-c8de-497f-a9ec-a37260c65"
},
{
"relative_scheduled_days": 0,
"status": "NOT_APPLICABLE",
"uid": "fb717b18-3a44-4488-b51e-1ff0af239"
},
{
"relative_scheduled_days": 1,
"status": "NOT_APPLICABLE",
"uid": "3a89301a-5bd6-4ecb-ac75-8f112969"
},
{
"relative_scheduled_days": 3,
"status": "NOT_APPLICABLE",
"uid": "6ea62a6f-96e4-40df-ace2-50427fc22f"
}
],
"request_type": "INSTALLMENT",
"tipping_enabled": false,
"total_completed_amount_money": {
"amount": 86000,
"currency": "AUD"
},
"uid": "adccbcb9-0acb-4459-99d9-6b107c5c2"
},
{
"automatic_payment_source": "NONE",
"computed_amount_money": {
"amount": 50000,
"currency": "AUD"
},
"due_date": "2024-07-20",
"fixed_amount_requested_money": {
"amount": 50000,
"currency": "AUD"
},
"reminders": [
{
"relative_scheduled_days": -7,
"status": "NOT_APPLICABLE",
"uid": "27ec1301-509a-4fbe-acd0-2b7d93a04b10"
},
{
"relative_scheduled_days": 0,
"status": "PENDING",
"uid": "36c50af6-59c9-46b5-90d0-43f2a76a6698"
},
{
"relative_scheduled_days": 1,
"status": "PENDING",
"uid": "5affd51b-b2f5-465e-be84-3dc3aa919d5a"
},
{
"relative_scheduled_days": 3,
"status": "PENDING",
"uid": "e9451e34-2fce-4e9c-ad58-07242da9a402"
}
],
"request_type": "INSTALLMENT",
"tipping_enabled": false,
"total_completed_amount_money": {
"amount": 0,
"currency": "AUD"
},
"uid": "d1e6aed5-779e-4434-89e3cc1645f6de"
}
]
This is the array I'm sending to iterator and want to get the value of "computed_amount_money" and add them up.
After sending this here, got an idea and it worked. If anyone comes across similar issue, use a code block to iterate through the json and compute the sum (or do any operations). Though I'd still be interested in knowing how to use iterator element in this situation as a no code solution.
Thanks for the update @Jithin, you can view the short video provided to learn how to add the amounts simultaneously for all the line items.
https://drive.google.com/file/d/1ne2VwgrwMlmCRK_5A9VcpbpRoP-sIw8e/view?usp=drivesdk
Thanks for the update @Jithin, you can view the short video provided to learn how to add the amounts simultaneously for all the line items.https://drive.google.com/file/d/1ne2VwgrwMlmCRK_5A9VcpbpRoP-sIw8e/view usp=drivesdk
Thank you for this method. First time checking out Data transformers. Though code block looks like the solution as it removes so many in between steps.
Ok @Jithin, thanks for the update. I hope the code by Pabbly works fine for you.