0

I have the below json file

[
    {
        "n_id": 1,
        "n_key": "sdfvnifughsdfvnsdfvnsd",
        "entries": 7
    },
    {
        "n_id": 2,
        "n_key": "amfgjbongmikjmjegieojgem",
        "entries": 8
    },
    {
        "n_id": 3,
        "n_key": "fdsggjhndsjfsjinfjghbdpojgd",
        "entries": 10
    }
]

How can I sum up all the entries in the above json file and save it in a variable JSON_SUM ??

I have zero knowledge in working with a json file, can someone please help me. I want to use this in a bash script

1 Answer 1

3

Like this:

$ json_sum=$(jq '[ .[] | .entries ] | add' file.json)
$ echo $json_sum
25
1
  • 1
    [ .[] | .entries ] can also be written as map(.entries) Nov 16, 2022 at 18:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .