Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Return JSON object with a "header"

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": "TEST@test.dk",
    "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": "Jeppe6721@gmail.com",
    "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": "sidra200265@gmail.com",
    "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
}

]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading