Home > other >  Return JSON object with a "header"
Return JSON object with a "header"

Time:02-01

I have an API, where I need to return data. I want to make a "header", where I specify the amount of tutors, the call returned. My current function looks like this:

def get_matching_tutors(filters):
formatted_tutors = []

matching_tutors = [tutor for tutor in tutor_list if matches_all_filters(filters, tutor)]

matching_tutors = sorted(matching_tutors, key = lambda i: i['hours'])
matching_tutors = sorted(matching_tutors, key = lambda i: i['tutor_amount_of_students'])

for tutor in matching_tutors:
    data = {
        "first_name": tutor["first_name"],
        "last_name": tutor["last_name"],
        "mobile_phone": tutor["mobile_phone"],
        "email": tutor["email"],
        "status": tutor["status"],
        "more_courses": tutor["more_courses"],
        "hours": tutor["hours"],
        "tutor_amount_of_students": tutor["tutor_amount_of_students"],
        "teachworks": tutor["teachworks"]
    }
    formatted_tutors.append(data)
return jsonify(formatted_tutors)

This is an example, of the response.

[
{
    "email": "[email protected]",
    "first_name": "TEST",
    "hours": 0,
    "last_name": "TEST",
    "mobile_phone": "",
    "more_courses": "Yes",
    "status": "Active",
    "teachworks": "https://toptutors.teachworks.com/employees/114106",
    "tutor_amount_of_students": 3
},
{
    "email": "[email protected]",
    "first_name": "Jeppe",
    "hours": 28.0,
    "last_name": "Vestergaard Nielsen",
    "mobile_phone": "51781003",
    "more_courses": "Yes",
    "status": "Active",
    "teachworks": "https://toptutors.teachworks.com/employees/133813",
    "tutor_amount_of_students": 3
},
{
    "email": "[email protected]",
    "first_name": "Sidra",
    "hours": 52.0,
    "last_name": "Ahmed",
    "mobile_phone": "52111671",
    "more_courses": "Yes",
    "status": "Active",
    "teachworks": "https://toptutors.teachworks.com/employees/133751",
    "tutor_amount_of_students": 3
}

]

I'd like to add a header, that specifies the amount of tutors/arrays that the response returned. So it would be at the top of the json response, "amount_of_tutors": (Amount)

CodePudding user response:

The simplest way is to add this line after the loop and change the return statement:

my_response = {'amount_of_tutors':len(formatted_tutors), 'tutors':formatted_tutors}
return jsonify(my_response)

But it is doubtful whether there is benefit having that extra key.

CodePudding user response:

You could make the "header" be the first item in formatted_tutors:

formatted_tutors = []
formatted_tutors.append({"amount_of_tutors": 0})

Then after the loop is finished, go back and update the "header" to contain the real number:

formatted_tutors[0]["amount_of_tutors"] = real_amount_of_tutors

But really, this seems unnecessary. What is the point of this extra header? Anyone calling this API will have to fully parse the json response, at which point they can just use len() to see how many tutors were returned. I don't see how the extra header is helpful.

  •  Tags:  
  • Related