In shell script, I have below array of object:
response={
"product": "BIG MAC",
"objects": [
{
"qty": 10,
"size": 32
},
{
"qty": 20,
"size": 53
},
{
"qty": 10,
"size": 54
}
]
}
I am writing a jq function to get total quantity.
data=$( echo $response | jq '.objects[] | .qty ' )
This gives me one line of quantities, like
10 20 10
How do I sum these to get value:
40
Create an array and pipe that through add
:
jq '[.objects[] | .qty] | add'
Using map
might make this simpler, as you don't have to "index" .objects
first:
jq '.objects | map(.qty) | add'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With