# Sum/aggregate expression on a array of json data property?

**URL:** <https://forum.inductiveautomation.com/t/sum-aggregate-expression-on-a-array-of-json-data-property/32638>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [February 26, 2020, 6:22pm UTC](https://forum.inductiveautomation.com/t/sum-aggregate-expression-on-a-array-of-json-data-property/32638 "2020-02-26T18:22:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [February 26, 2020, 6:22pm UTC](https://forum.inductiveautomation.com/t/sum-aggregate-expression-on-a-array-of-json-data-property/32638/1 "2020-02-26T18:22:16Z")

</div>

I have a table in perspective that’s data property is a array of json objects. We do this so we’re able to manipulate row background colors.

In this same table, I want to get a sum of a column, in this case, a boolean column named “Yes” with a check box to count how many were checked. I am using `sum({this.props.data}, "Yes")` but this is not calculating the correct value.

Do these aggregate functions only work on actual dataset type data properties? Is there a way around this or will I have to resort to some scripting on a table extension function?

---

<div class="post-metadata">

**Author:** ![kirsten.chambers](https://avatars.discourse-cdn.com/v4/letter/k/48db29/32.png) [@kirsten.chambers](https://forum.inductiveautomation.com/u/kirsten.chambers)\
**Post date:** [February 26, 2020, 7:22pm UTC](https://forum.inductiveautomation.com/t/sum-aggregate-expression-on-a-array-of-json-data-property/32638/2 "2020-02-26T19:22:14Z")

</div>

According to the user manual, [`sum`](https://docs.inductiveautomation.com/display/DOC80/sum) only accepts datasets, or an arbitrary amount of ints/floats as parameters to sum up, and not json data.

The script to solve your problem should be very simple, perhaps a custom property on your table with a binding onto `props.data`–something like this:

```auto
# given this is in, say, a property binding on a 
# custom property which references your table's props.data
sum = 0
for row in value: # 'value' is your table's props.data
    if row["Yes"] == True:
        sum += 1
return sum

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [February 26, 2020, 8:11pm UTC](https://forum.inductiveautomation.com/t/sum-aggregate-expression-on-a-array-of-json-data-property/32638/3 "2020-02-26T20:11:58Z")

</div>

At some point in the future, we’re planning to have the aggregate expression functions work on json objects and arrays, but it’s a non-trivial amount of refactoring to make sure it doesn’t break other things.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [February 26, 2020, 8:15pm UTC](https://forum.inductiveautomation.com/t/sum-aggregate-expression-on-a-array-of-json-data-property/32638/4 "2020-02-26T20:15:42Z")

</div>

Good to know. I tried doing a `sum(toDataSet({this.props.data}),"Yes")` as an expression, but realized the toDataSet just turned it into a dataset with a single column of qualified values.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [June 2, 2023, 7:01pm UTC](https://forum.inductiveautomation.com/t/sum-aggregate-expression-on-a-array-of-json-data-property/32638/5 "2023-06-02T19:01:01Z")

</div>

Interested in the status of this improvement 🙂

When looping over the filter results on a perspective table to sum a column is ugly.

```auto
for row in currentValue.value:
	addon += row.value['addon'].value

```

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [June 2, 2023, 7:37pm UTC](https://forum.inductiveautomation.com/t/sum-aggregate-expression-on-a-array-of-json-data-property/32638/6 "2023-06-02T19:37:26Z")

</div>

```auto
sum(row['addon'] for row in currentValue.value)

```

Assuming that `currentValue.value` is an Array of Objects

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [June 2, 2023, 7:39pm UTC](https://forum.inductiveautomation.com/t/sum-aggregate-expression-on-a-array-of-json-data-property/32638/7 "2023-06-02T19:39:38Z")

</div>

Yeah, that is a cleaner way to write it for sure, but it's not an array of objects, I think it is an array of qualified values.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [June 2, 2023, 7:43pm UTC](https://forum.inductiveautomation.com/t/sum-aggregate-expression-on-a-array-of-json-data-property/32638/8 "2023-06-02T19:43:24Z")

</div>

Interesting....where is this script? Is it a property change?

The data prop on a perspective table (as far as I can tell) can only be a Dataset, an Array of Arrays, or an Array of Objects.

EDIT: Looks like in a property change event, it is, as you say converted into an Array of qualified values. I would not have expected that.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [June 2, 2023, 8:18pm UTC](https://forum.inductiveautomation.com/t/sum-aggregate-expression-on-a-array-of-json-data-property/32638/9 "2023-06-02T20:18:22Z")

</div>

It's on the `props.filter.results.data` and yes it's a change event script.
