Home > Software engineering >  JSON confusion.... (new to this!)
JSON confusion.... (new to this!)

Time:11-06

Looking to knock together some Python to make sense of some JSON data I'm pulling from an API. I'll paste a sample of the JSON below. All I'm looking to do at this stage is count up the totals of each status code. I've tried to use the complex JSON "todos" example on jsonplaceholder but this JSON I'm pulling is a little more complex than the sample provided there so I'm a little stuck.

I have code that just does a 'find' in the text stream, and it seems to work OK. I'd just rather learn and use proper methods to parse the JSON data natively.

Looking at the JSON, the stuff that I want is under "results". It looks I need to somehow extract whats under that into it's own new object - that will then look like the JSON shown in the example above and I'll be good to go!

Cheers!

`

{
  "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": {}
}

`

Tried allsorts! Nothing doing what I want!

CodePudding user response:

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}
  • Related