Q:

Get individual elements from a json

I have a json response that looks like so:
{
    "Matches":[
        {
            "id": 1,
            "Teams": [
                {"id": 1, "score": 254},
                {"id": 2, "score": 253}
            ]
        },
        {
            "id": 2,
            "Teams": [
                {"id": 1, "score": 330},
                {"id": 3, "score": 331}
            ]
        },
        {
            "id": 3,
            "Teams": [
                {"id": 1, "score": 220},
                {"id": 4, "score": 220}
            ]
        },
        {
            "id": 4,
            "Teams": [
                {"id": 1, "score": 264},
                {"id": 5, "score": 189}
            ]
        },
        {
            "id": 5,
            "Teams": [
                {"id": 2, "score": 240},
                {"id": 3, "score": 200}
            ]
        },
        {
            "id": 6,
            "Teams": [
                {"id": 2, "score": 330},
                {"id": 4, "score": 331}
            ]
        },
        {
            "id": 7,
            "Teams": [
                {"id": 2, "score": 320},
                {"id": 5, "score": 220}
            ]
        },
        {
            "id": 8,
            "Teams": [
                {"id": 3, "score": 320},
                {"id": 4, "score": 300}
            ]
        },
        {
            "id": 9,
            "Teams": [
                {"id": 3, "score": 280},
                {"id": 5, "score": 300}
            ]
        },
        {
            "id": 10,
            "Teams": [
                {"id": 4, "score": 180},
                {"id": 5, "score": 180}
            ]
        }
    ]
}

And this is the struct I have for the above response:
struct Matches: Codable {
    let matches: [Match]

    enum CodingKeys: String, CodingKey {
        case matches = "Matches"
    }
}

// MARK: - Match
struct Match: Codable {
    let id: Int
    let teams: [Team]

    enum CodingKeys: String, CodingKey {
        case id
        case teams = "Teams"
    }
}

// MARK: - Team
struct Team: Codable {
    let id, score: Int
}

And this is how I'm making the api call to get the data:
fileprivate func getMatches() {
    URLSession.shared.dataTask(with: URL(string: "URL HERE")!) { (data, response, error) in
        guard let data = data else { return }
        do {
            let res = try JSONDecoder().decode(Matches.self, from: data)

        }
        catch {
            print(error)
        }
        }.resume()
}

But what I'm not able to figure out is how I can compare the scores of 2 id's within the Teams in the json response and say which id is greater and also the count of each id's..

A:

Matches of team 1:
let team1 = res.matches.filter {$0.teams.contains { $0.id == 1 }}

Find the winner of each match, use this extension:
extension Match {
    var winnerTeam: Team {
        return teams.sorted { $0.score > $1.score }.first!
    }
}

for example the array of winners would be like:
let winners = res.matches.map { $0.winnerTeam }

Similarly for finding looser teams:
extension Match {
    var looserTeam: Team {
        return teams.sorted { $0.score > $1.score }.last!
    }
}

For counting winner teams by id:
func scoresOfWinners() -> [Int: Int] {
    var scores = [Int: Int]()
    for team in winners {
        scores[team.id] = (scores[team.id] ?? 0) + 1
    }
    return scores
}

it will return a list of id: win count.
Remember if you want to handle tie situation, you should guard before picking winner or looser like this:
var winnerTeam: Team? {
    guard teams.first!.score != teams.last!.score else { return nil }
    return teams.sorted { $0.score > $1.score }.first!
}

