# AVG all keys of an array of objects

**URL:** <https://discourse.nodered.org/t/avg-all-keys-of-an-array-of-objects/69378>\
**Category:** General\
**Created:** [21 October 2022 12:58 UTC](https://discourse.nodered.org/t/avg-all-keys-of-an-array-of-objects/69378 "2022-10-21T12:58:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![protonmw](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@protonmw](https://discourse.nodered.org/u/protonmw)\
**Post date:** [21 October 2022 12:58 UTC](https://discourse.nodered.org/t/avg-all-keys-of-an-array-of-objects/69378/1 "2022-10-21T12:58:16Z")

</div>

Hi all,

I need to average all keys of an array of objects like this:

```auto
[{"a":-5736,"b":6436,"c":15888,"d":32700,"e":18338},{"a":-5736,"b":6436,"c":15888,"d":32700,"e":18338},{"a":-5738,"b":6436,"c":15888,"d":32700,"e":18340},{"a":-5743,"b":6436,"c":15884,"d":32700,"e":18337},{"a":-5742,"b":6436,"c":15888,"d":32700,"e":18338}]

```

How can I do this in JASONata?  
Number of different keys will be unknown/dynamic in later apllication.

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [21 October 2022 13:47 UTC](https://discourse.nodered.org/t/avg-all-keys-of-an-array-of-objects/69378/2 "2022-10-21T13:47:34Z")

</div>

See:

- [https://try.jsonata.org/FRHbn1qTe](https://try.jsonata.org/FRHbn1qTe)

So the JSONata expression:

```auto
(payload~>$keys()).(
    $key := $;
    { $key : $$.payload~>$lookup($key)~>$average()
    }
)~>$merge()

```

I am assuming that the array is stored in `payload`.

So it produces following output:

```auto
{
  "a": -5739,
  "b": 6436,
  "c": 15887.2,
  "d": 32700,
  "e": 18338.2
}

```

for input message:

```auto

{ "payload" : [
  {
    "a": -5736,
    "b": 6436,
    "c": 15888,
    "d": 32700,
    "e": 18338
  },
  {
    "a": -5736,
    "b": 6436,
    "c": 15888,
    "d": 32700,
    "e": 18338
  },
  {
    "a": -5738,
    "b": 6436,
    "c": 15888,
    "d": 32700,
    "e": 18340
  },
  {
    "a": -5743,
    "b": 6436,
    "c": 15884,
    "d": 32700,
    "e": 18337
  },
  {
    "a": -5742,
    "b": 6436,
    "c": 15888,
    "d": 32700,
    "e": 18338
  }
]
}

```

---

<div class="post-metadata">

**Author:** ![protonmw](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@protonmw](https://discourse.nodered.org/u/protonmw)\
**Post date:** [21 October 2022 13:50 UTC](https://discourse.nodered.org/t/avg-all-keys-of-an-array-of-objects/69378/3 "2022-10-21T13:50:09Z")

</div>

Wow! Thanks!  
I think I never got this solution on my own.. 🤔

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [21 October 2022 14:11 UTC](https://discourse.nodered.org/t/avg-all-keys-of-an-array-of-objects/69378/4 "2022-10-21T14:11:02Z")

</div>

I have broken down the problem in 3 steps:

1. construct an array of keys.
2. transform this into an array of { _key_ : _average for that key_ } objects
3. merge this array into json object

Using the jonata exerciser you just have to delete some part of the expression to see how it works.

- [https://try.jsonata.org/FRHbn1qTe](https://try.jsonata.org/FRHbn1qTe)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [4 November 2022 14:11 UTC](https://discourse.nodered.org/t/avg-all-keys-of-an-array-of-objects/69378/5 "2022-11-04T14:11:06Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
