Search code examples
jsonpython-3.xiterationdeserialization

How to iterate through deserialized JSON to count specific values


Using Python, got the JSON below from an API response.

All I'm looking to do at this stage is count up the totals of each status code.

I've tried to implement the counting of ToDos from this Real Python article but this JSON I'm pulling is a little more complex than the sample provided there so I'm a little stuck.

Looking at the JSON, the stuff that I want is under "results".

I'd like to learn and use proper methods to parse the JSON data.

{
  "results": [
    {
      "accepted": true,
      "relevant_via_survey": true,
      "status": "TS1",
      "status_updated": "2022-09-30T05:57:27.675000-04:00",
      "task_id": "T178",
    },
    {
      "accepted": true,
      "relevant_via_survey": true,
      "status": "TS2",
      "status_updated": null,
      "task_id": "T1213",
    },
    {
      "accepted": true,
      "relevant_via_survey": true,
      "status": "TS1",
      "status_updated": null,
      "task_id": "T1188",
    },
    {
      "accepted": true,
      "relevant_via_survey": true,
      "status": "TS2",
      "status_updated": null,
      "task_id": "T1177",
    },
    {
      "accepted": true,
      "relevant_via_survey": true,
      "status": "TS1",
      "status_updated": null,
      "task_id": "T554",
    },
    {
      "accepted": true,
      "relevant_via_survey": true,
      "status": "TS2",
      "status_updated": null,
      "task_id": "T1539",
    },
    {
      "accepted": true,
      "relevant_via_survey": true,
      "status": "TS3",
      "status_updated": null,
      "task_id": "T106",
    },
    {
      "accepted": true,
      "relevant_via_survey": true,
      "status": "TS2",
      "status_updated": null,
      "task_id": "T65",
    }
  ],
  "facets": {}
}

Solution

  • I definitely recommend reading more about:

    • JSON serialization/deserialization.
    • Python Dictionaries
    • Python Lists

    When you bring in the API response, the object will be a dictionary. I've called it api_response below.

    You can target the list of results like this: results = api_response['results'] (see below).

    Now that you have the results list, you can iterate through it and implement the counting technique that you linked to from Real Python's JSON example of counting TODOs.

    import json
    
    
    api_response = {
        "results": [
            {
                "accepted": True,
                "relevant_via_survey": True,
                "status": "TS1",
                "status_updated": "2022-09-30T05:57:27.675000-04:00",
                "task_id": "T178",
            },
            {
                "accepted": True,
                "relevant_via_survey": True,
                "status": "TS2",
                "status_updated": None,
                "task_id": "T1213",
            },
            {
                "accepted": True,
                "relevant_via_survey": True,
                "status": "TS1",
                "status_updated": None,
                "task_id": "T1188",
            },
            {
                "accepted": True,
                "relevant_via_survey": True,
                "status": "TS2",
                "status_updated": None,
                "task_id": "T1177",
            },
            {
                "accepted": True,
                "relevant_via_survey": True,
                "status": "TS1",
                "status_updated": None,
                "task_id": "T554",
            },
            {
                "accepted": True,
                "relevant_via_survey": True,
                "status": "TS2",
                "status_updated": None,
                "task_id": "T1539",
            },
            {
                "accepted": True,
                "relevant_via_survey": True,
                "status": "TS3",
                "status_updated": None,
                "task_id": "T106",
            },
            {
                "accepted": True,
                "relevant_via_survey": True,
                "status": "TS2",
                "status_updated": None,
                "task_id": "T65",
            }
        ],
        "facets": {}
    }
    
    
    results = api_response['results']
    
    count_by_status = {}
    
    for result in results:
        try:
            count_by_status[result['status']] += 1
        except KeyError:
            count_by_status[result['status']] = 1
    
    print(count_by_status)
    

    The output will be:

    {'TS1': 3, 'TS2': 4, 'TS3': 1}