How can I get the sum of objects count inside all movements?
Currently I'm chewing without success on this snippet below, though all my attempts digging down the nodes fail.
import json
RESULTS = 'sample.json'
with open(RESULTS) as f:
data = json.load(f)
results = data
print(len(results[0]['result']['movements'])) #Expected: 3
sample.json
[
{
"result": {
"TempId": "369477387",
"movements": [
{
"date": "2018-05-03",
"credit": 100.0,
"shorttext": "My<br/>Expense "
},
{
"date": "2018-05-03",
"debit": 200.0,
"shorttext": "My<br/>F\u00dcR"
}
]
}
},
{
"result": {
"TempId": "369477395",
"movements": []
}
},
{
"result": {
"TempId": "369477402",
"movements": [
{
"date": "2018-05-07",
"credit": 100.0,
"shorttext": "My<br/>Expense "
}
]
}
}
]
Using a list comprehension you can find out the number of movements
from every element from sample list.
Then just apply sum
method over count
list in order to find out the sum.
count = sum([len(item['result']['movements']) for item in sample])
Output
>> count
3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.