skip to Main Content

I have a nested json response from a vendors API.

{
    "data":{
        "id":"hidden",
        "type":"file_import",
        "attributes":{
            "id":"hidden",
            "type":"Academics API",
            "dry_run":false,
            "status":"completed",
            "import_started_at":"2023-09-05T15:13:59.159Z",
            "import_completed_at":"2023-09-05T15:14:11.581Z",
            "result":{
                "info":{
                    "terms":[],
                    "teachers":{
                        "created":{
                            "total":0,
                            "list":[]
                        },
                        "updated":{
                            "total":0,
                            "list":[                               
                            ]
                        }
                    },
                    "courses":{
                        "created":{
                            "total":0,
                            "list":[]
                        },
                        "updated":{
                            "total":0,
                            "list":[                                
                            ]
                        }
                    },
                    "enrollments":{
                        "updated":{
                            "total":0
                        },
                        "students":{
                            "created":{
                                "total":0
                            },
                            "updated":{
                                "total":0
                            }
                        }
                    }
                },
                "warnings":{
                    "terms":[],
                    "teachers":[],
                    "courses":[],
                    "enrollments":[
                        {
                            "type":"duplicate_school_ids",
                            "message":"The following school ids are connected to multiple students and were ignored",
                            "list":[
                                "12345678",
                                "87654321",
                                "11122233",
                                "44455566",
                                "77788899"
                            ]
                        },
                        {
                            "type":"missing_students",
                            "message":"The following Students were not found in the school's students list and were ignored.",
                            "list":[
                                "65437898",
                                "77788899"
                            ]
                        }
                    ]
                },
                "errors":{
                    "general":[],
                    "original_data":[],
                    "terms":[],
                    "teachers":[],
                    "courses":[],
                    "enrollments":[]
                }
            }
        }
    }
}

I’m trying to get to warnings and capture each type. So far I’m only seeing enrollment warning such as duplicate_school_ids, and missing_students. I’m assuming I will always have type, message, and list for each type warning, for example

"terms": [
            {
              "type": "missing_terms",
              "message": "The following terms were not found in the school's terms list and were ignored",
              "list": [
                "Spring 2021",
                "Spring 2022"
              ]
            } 

The end result is to put this into an email after the process completes and send it to department staff to fix the data issues.

I’m thinking to create four empty list and populate them with what is found in the json reponse.

self.term_warnings: list[TermWarnings] = []
self.enrollment_warnings: list[EnrollmentWarnings] = []
self.course_warnings: list[CourseWarnings] = []
self.teacher_warning: list[TeacherWarnings] = [] 

Then check the length of each to see whether they are empty or I need to dump them into an email.

So my code is rough but look like this.

f = open("sample_result.json", "r")
data = json.load(f)

warnings = data["data"]["attributes"]["result"]
for w in warnings["warnings"]:
    for t in w["enrollments"]:
        for emplid in t["list"]:
            #print(f"Warning type:{t}, Student - {emplid}")
            self.enrollment_warnings.append(
                EnrollmentWarnings({t},{emplid})

My code doesn’t work with this error.

TypeError: string indices must be integers

Any help will be appreciated.

2

Answers


  1. for w in warnings["warnings"]:
    

    warnings["warnings"] is a dictionary. Iterating over a dictionary gives you the keys of the dictionary. So in the first iteration of the above loop, w is the string "terms", because that is the first key in the dictionary.

    Then for t in w["enrollments"]: gives you the error, because w is a string.

    You already know exactly what keys are present in the warnings dictionary, so you don’t need to iterate over that dictionary itself. Instead, iterate over the known sub-lists:

    for term in warnings["terms"]:
        ...
    for teacher in warnings["teachers"]:
        ...
    for course in warnings["courses"]:
        ...
    for enrollment in warnings["enrollments"]:
        ...
    
    Login or Signup to reply.
  2. You’re iterating over the dictionary keys with for w in warnings["warnings"].

    Since you only want to iterate over the enrollments, you need another level of dictionary access:

    warnings = data["data"]["attributes"]["result"]["warnings"]
    for t in warnings["enrollments"]:
        for emplid in t["list"]:
            #print(f"Warning type:{t['type']}, Student - {emplid}")
            self.enrollment_warnings.append(
                EnrollmentWarnings({t},{emplid})
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search