Hi all,
I need to average all keys of an array of objects like this:
[{"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.
janvda
2
See:
So the JSONata expression:
(payload~>$keys()).(
$key := $;
{ $key : $$.payload~>$lookup($key)~>$average()
}
)~>$merge()
I am assuming that the array is stored in payload
.
So it produces following output:
{
"a": -5739,
"b": 6436,
"c": 15887.2,
"d": 32700,
"e": 18338.2
}
for input message:
{ "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
}
]
}
1 Like
Wow! Thanks!
I think I never got this solution on my own.. 
janvda
4
I have broken down the problem in 3 steps:
- construct an array of keys.
- transform this into an array of { key : average for that key } objects
- 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.
1 Like
system
Closed
5
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.