0

This is the output I receive via curl. I want to save 'playcount' in a variable. How do I loop through tracks[] to get the strings 'playcount' and save them so I can form the sum?
I've spend a lot of time figuring it out with jq but nothing really worked out..

{
  "success": true,
  "data": {
    "track_count": 2,
    "discs": [
      {
        "number": 1,
        "name": "",
        "tracks": [
          {
            "album_id": "u2hh2n2b7fo20v46cbe",
            "playcount": 17212,
            "name": "World Savior",
            "number": 1,
            "duration": 341,
          },
          {
            "album_id": "jk299sdhjahj991nbwd",
            "playcount": 9812,
            "name": "Tower",
            "number": 2,
            "duration": 281,
          }
        ]
      }
    ],
    "month": 11,
    "day": 26,
    "year": 2021,
    "type": "single",
  }
}

1 Answer 1

2

If you want the list of playcount values, you just have to drill down to the right content:

jq '.data.discs[].tracks[].playcount'

If you want their sum, make an array of that and add its entries:

jq '[.data.discs[].tracks[].playcount]|add'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.