Home > database >  get distinct values in JSON object
get distinct values in JSON object

Time:02-22

I need to get distinct teams in the JSON object which I filter. using the below code I'm able to filter and get the required JSON object but I need distinct teams inside the JSON object and also count should be changed. highly appreciate your support.

Sample Input JSON array

 [
  {
    "team": "Alco",
    "eligible": "Yes",
    "repo": "MaticCorporation/xt-service",
    "tlt_division": "Commercial Group"
  },
  {
    "team": "Alco",
    "eligible": "Yes",
    "repo": "MaticCorporation/xr-service-new",
    "tlt_division": "Commercial Group"
  },
  {
    "team": "Alco",
    "eligible": "Yes",
    "repo": "MaticCorporation/cx-service-v2",
    "tlt_division": "Commercial Group"
  },
  {
    "team": "Plutas",
    "eligible": "Yes",
    "repo": "MaticCorporation/sn-api",
    "tlt_division": "Commercial Group"
  },
  {
    "team": "Afin",
    "eligible": "Yes",
    "repo": "MaticCorporation/rt-history",
    "tlt_division": "Commercial Group"
  },
  {
    "team": "EAD-Trufin",
    "eligible": "Yes",
    "repo": "MaticCorporation/ead-api",
    "tlt_division": "EAD"
  }
]

Code:

def team_by_tlt(repo_list):
    tlt_teams = {}
    for team in repo_list:
        if team['tlt_division'] not in tlt_teams:
            tlt_teams[team['tlt_division']] = []
        tlt_teams[team['tlt_division']].append(team['team'])


    result = []
    for tlt, team_list in tlt_teams.items():
        result.append({
            "tlt_division": tlt,
            "team_count": len(team_list),
            "teams": team_list
        })

    return result

Actual Output

[
  {
    "tlt_division": "Commercial Group",
    "team_count": 5,
    "teams": [
      "Alco",
      "Alco",
      "Alco",
      "Plutas",
      "Afin"
    ]
  },
  {
    "tlt_division": "EAD",
    "team_count": 1,
    "teams": [
      "ead-Trufin"
    ]
  }
]

Expected Output

[
  {
    "tlt_division": "Commercial Group",
    "team_count": 3,
    "teams": [
      "Alco",
      "Plutas",
      "Afin"
    ]
  },
  {
    "tlt_division": "EAD",
    "team_count": 1,
    "teams": [
      "ead-Trufin"
    ]
  }
]

CodePudding user response:

try this solution:

import json

def team_by_tlt(obj):
    for k in obj:
        result = {k: []}
        for r in obj[k]:
            r['teams'] = list(dict.fromkeys(r['teams']))
            r['team_count'] = len(r['teams'])
            result[k].append(r)
    return result


with open('U:\ProjetCSharp\Edit1.json') as json_file:
    data = json.load(json_file)

data = team_by_tlt(data)
print(json.dumps(data, sort_keys=False, indent=4))

result:

{
    "team_by_tlt_division": [
        {
            "tlt_division": "Commercial Group",
            "team_count": 7,
            "teams": [
                "Alco",
                "Keldo",
                "Ninco",
                "Ritas",
                "Atil",
                "Rina",
                "Perti"
            ]
        },
        {
            "tlt_division": "STS",
            "team_count": 1,
            "teams": [
                "ess-turn"
            ]
        }
    ]
}

CodePudding user response:

I could find solution. What i did is I've use set function. here is modified code

def team_by_tlt(repo_list):
    tlt_teams = {}
    for team in repo_list:
        if team['tlt_division'] not in tlt_teams:
            tlt_teams[team['tlt_division']] = []
        tlt_teams[team['tlt_division']].append(team['team'])

    result = []
    for tlt, team_list in tlt_teams.items():
        result.append({
            "tlt_division": tlt,


            "team_count": len(set(team_list)),
            "teams": set(team_list)


        })
        
    return result
  • Related