0

I have a query like this:

curl -s \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_TOKEN" \
--data "$(echo $PAYLOAD)" \
https://myapi/client/v4/graphql/ | jq .

... that produces output like this:

{
  "data": {
    "viewer": {
      "accounts": [
        {
          "magicTransitTunnelTrafficAdaptiveGroups": [
            {
              "avg": {
                "bitRateFiveMinutes": 23360
              },
              "dimensions": {
                "datetimeFiveMinutes": "2022-12-30T09:00:00Z",
                "tunnelName": "nw-blue"
              }
            },
            {
              "avg": {
                "bitRateFiveMinutes": 8960
              },
              "dimensions": {
                "datetimeFiveMinutes": "2022-12-30T09:00:00Z",
                "tunnelName": "mtlab_gcp1"
              }
            },
            {
              "avg": {
                "bitRateFiveMinutes": 95493
              },
              "dimensions": {
                "datetimeFiveMinutes": "2022-12-30T09:00:00Z",
                "tunnelName": "Cherry_CBE_1"
              }
            },
            {
              "avg": {
                "bitRateFiveMinutes": 2968507
              },
              "dimensions": {
                "datetimeFiveMinutes": "2022-12-30T09:00:00Z",
                "tunnelName": "Cherry_VCB"
              }
            },
            {
              "avg": {
                "bitRateFiveMinutes": 10880
              },
              "dimensions": {
                "datetimeFiveMinutes": "2022-12-30T09:00:00Z",
                "tunnelName": "accelia-poc1"
              }
            },
            {
              "avg": {
                "bitRateFiveMinutes": 21227
              },
              "dimensions": {
                "datetimeFiveMinutes": "2022-12-30T09:00:00Z",
                "tunnelName": "mtlab_sr_pni"
              }
            },
            {
              "avg": {
                "bitRateFiveMinutes": 27627
              },
              "dimensions": {
                "datetimeFiveMinutes": "2022-12-30T09:00:00Z",
                "tunnelName": "mtlab-tme-gcp1"
              }
            }
          ]
        }
      ]
    }
  },
  "errors": null
}

I want to parse all the bitRateFiveMinutes values, add them up, and stuff them into a variable for evaluation. How do I do this using the jq -r option? I looked at the examples on https://stedolan.github.io/jq/manual/, but I could not determine what to use. It looks like there is a built-in operator + that might do the addition for me as well.

1 Answer 1

1

Here I'm taking a bit of a shortcut to avoid having to write out the complete path to the wanted values. I put the values in an array and then I run it through add to sum it up:

curl -s ... |
jq '[ .data[][][][][].avg.bitRateFiveMinutes ] | add'

Given the example in your question, this would output 3156054. You don't need to use jq with its -r option here, as integers are not string encoded.

A more verbose jq command would possibly look like

curl -s ... |
jq '.data.viewer.accounts |
    map(.magicTransitTunnelTrafficAdaptiveGroups |
        map(.avg.bitRateFiveMinutes) | add) | add'

This first computes the sum over all entries of each magicTransitTunnelTrafficAdaptiveGroups array and then adds these partial sums together. In your example, there's only a single of these inner arrays.

Getting the result into a shell variable is then a matter of running the pipeline in a command substitution and assigning it to a variable:

avgsum=$(
    curl -s ... |
    jq '[ .data[][][][][].avg.bitRateFiveMinutes ] | add'
)

Also note that your --data "$(echo $PAYLOAD)" is better written as --data "$PAYLOAD", unless you need to have the shell perform splitting and filename globbing on the unquoted value of $PAYLOAD, and echo to do its special processing of \n, \t, and \\.

4
  • Thank you so much for your thorough answer. This helped immensley!
    – Jeff Sani
    Commented Jan 3, 2023 at 15:46
  • I did try to remove echo from the curl query, but got this error: jq: error (at <stdin>:0): Cannot iterate over null (null)
    – Jeff Sani
    Commented Jan 3, 2023 at 15:57
  • @JeffSani That's an error from jq, and I don't think that has anything to do with the echo. Look at the result of the curl request and whether the response looks very different from what you presented in the question (an array be empty, or other bits may be missing). If you still can't figure out what's happening, consider opening a new question. I'm unfamiliar with the service you're accessing with curl, so I can't say what the expected behaviour would be.
    – Kusalananda
    Commented Jan 3, 2023 at 16:45
  • I was reviewing your answer again and wanted to know how you arrived at 5 brackets "[][][][][]" to get to the object and value I needed? Is it as simple as adding the curly brackets?
    – Jeff Sani
    Commented Feb 4, 2023 at 22:56

You must log in to answer this question.

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