list - How to get sum of all elements in json using Scala? -



list - How to get sum of all elements in json using Scala? -

i have next json-

"disks" : [ { "name" : "v2.16", "diskaggregate" : "aggr0", "diskrpm" : 15000, "totalsizebytes" : 1077477376, "vendorid" : "netapp ", "usedbytes" : 1070071808, "disktype" : "fcal", "uuid" : "4e455441:50502020:56442d31:3030304d:422d465a:2d353230:32353836:30303030:00000000:00000000", "portname" : "fc:a ", "raidgroup" : "rg0" }, { "name" : "v4.16", "diskaggregate" : "aggr0", "diskrpm" : 15000, "totalsizebytes" : 1077477376, "vendorid" : "netapp ", "usedbytes" : 1070071808, "disktype" : "fcal", "uuid" : "4e455441:50502020:56442d31:3030304d:422d465a:2d353230:32353633:34333030:00000000:00000000", "portname" : "fc:b ", "raidgroup" : "rg0" }]

i want add-on of usedbytes json objects json array 'disks'. tried out fold in scala didn't desired output. here code -

val datastorecapacity = disks val usablespace = datastorecapacity.foldleft(0l) { case (sumofusedspace, esxdevice) => val sumoftotalbytesonstoragedevice = esxdevice.datastores.foldleft(0l) { case (totalbytesondevice, datastore) => // totalbytesondevice + ut..getorelse(0l).tostring.tolong val sum = datastore.utilization.foldleft(0l) { case (total,util) => total + util.usedbytes.getorelse(0l).tostring.tolong } } sumofusedspace + sumoftotalbytesonstoragedevice }

how total of used bytes using scala?

assuming disks list of parsed objects getter called usedbytes returning alternative of long (as code suggests), should do:

disks.map(_.usedbytes).flatten.sum

some explanation:

with map transform objects given lambda function (which calls getter usedbytes).

flatten filters out nones , leaves values of somes in list. (can used on lists of lists or arrays of vectors etc. etc.)

sum says , builds sum of collection of numeric values (which result flatten)

guessing comment assume despite illustration info above, disks have attribute utilization. don't know looks like, , means because unclear question if assume 1 time again list disks. following

disks.flatmap(_.utilization.map(_.usedbytes).flatten).sum

please specify info want process if isn't looking for.

json list scala

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -