Your approach is correct, however you missed a tiny spot in the docs. ;)
In the docs for SUM() it says:
SUM(list) : returns the sum of the values in list. This requires the elements in list to be numbers. null values are ignored.
(Docs reference: https://www.arangodb.org/manuals/current/Aql.html#AqlFunctionsList)
Note the "This requires the elements in list to be numbers"
So, your JSON-example should have numbers in the list, instead of strings.
let json = {
"ages": {
"age": [
20,
30,
40
]
}
}
let ages = (for age in json.ages.age return age)
return sum(ages)
Copying and running the above snippet into your AQL editor should result in the desired:
[
90
]
You can of course also leave your json intact and type-cast the strings into numbers by changing the AQL query accordingly:
let json = {
"ages": {
"age": [
"20",
"30",
"40"
]
}
}
let ages = (for age in json.ages.age return TO_NUMBER(age))
return sum(ages)