Home > Mobile >  Set Default Name for JSON Key in Python
Set Default Name for JSON Key in Python

Time:11-06

I'm trying to search a JSON from an API. I'm trying to get names and IDs from the JSON. Some keys inside of the JSON do not have a name. Is there any way to set a default? Or is there any other way to delete the JSON values without names?

Code:

import requests
from pprint import pprint

url = "https://<SCHOOL CANVAS PAGE>.instructure.com/api/v1/courses"
headers = {"Authorization": "Bearer <AUTHORIZATION KEY>"}

r = requests.get(url, headers=headers).json()
pprint(r)


# for k in r:
#   print(f'Name: {k["name"]}')
#   --> If no name it returns "print(f'Name: {k["name"]}') | KeyError: 'name'"
# for k in r:
#   print(f'ID: {k["id"]}')

Api Output:

[
  {
    "id": 3941,
    "root_account_id": 1,
    "account_id": 9,
    "enrollment_term_id": 93,
    "name": "7th Period",
    "uuid": "8IGbnEvvHWK9beNGlI1uwGWLjv3R1hEIjExkY1kc",
    "start_at": "2019-08-07T17:55:35Z",
    "grading_standard_id": null,
    "is_public": null,
    "created_at": "2019-07-30T21:48:19Z",
    "course_code": "Social Studies Advanced 08 - Fickley",
    "default_view": "modules",
    "license": null,
    "grade_passback_setting": null,
    "end_at": null,
    "public_syllabus": false,
    "public_syllabus_to_auth": false,
    "storage_quota_mb": 500,
    "is_public_to_auth_users": false,
    "homeroom_course": false,
    "course_color": null,
    "friendly_name": null,
    "apply_assignment_group_weights": false,
    "calendar": {
      "ics": "https://clevelandschools.instructure.com/feeds/calendars/course_8IGbnEvvHWK9beNGlI1uwGWLjv3R1hEIjExkY1kc.ics"
    },
    "time_zone": "America/New_York",
    "original_name": "Social Studies Advanced 08 - Fickley",
    "blueprint": false,
    "template": false,
    "enrollments": [
      {
        "type": "student",
        "role": "StudentEnrollment",
        "role_id": 3,
        "user_id": 1546,
        "enrollment_state": "active",
        "limit_privileges_to_course_section": false
      }
    ],
    "hide_final_grades": false,
    "workflow_state": "available",
    "restrict_enrollments_to_course_dates": false,
    "overridden_course_visibility": ""
  },
  {
    "id": 10397,
    "root_account_id": 1,
    "account_id": 10,
    "name": "Algebra 2 H - Scurlock",
    "enrollment_term_id": 356,
    "uuid": "W6nOG0uwQGCbbgnIzHdJxT0fxp99wZUhLQxe6SFk",
    "start_at": "2021-07-22T13:32:44Z",
    "grading_standard_id": null,
    "is_public": null,
    "created_at": "2021-07-19T16:05:23Z",
    "course_code": "Algebra 2 H - Scurlock",
    "default_view": "modules",
    "license": null,
    "grade_passback_setting": null,
    "end_at": null,
    "public_syllabus": false,
    "public_syllabus_to_auth": false,
    "storage_quota_mb": 500,
    "is_public_to_auth_users": false,
    "homeroom_course": false,
    "course_color": null,
    "friendly_name": null,
    "apply_assignment_group_weights": false,
    "calendar": {
      "ics": "https://clevelandschools.instructure.com/feeds/calendars/course_W6nOG0uwQGCbbgnIzHdJxT0fxp99wZUhLQxe6SFk.ics"
    },
    "time_zone": "America/New_York",
    "blueprint": false,
    "template": false,
    "enrollments": [
      {
        "type": "student",
        "role": "StudentEnrollment",
        "role_id": 3,
        "user_id": 1546,
        "enrollment_state": "active",
        "limit_privileges_to_course_section": false
      }
    ],
    "hide_final_grades": false,
    "workflow_state": "available",
    "restrict_enrollments_to_course_dates": false
  },
  {
    "id": 5844,
    "root_account_id": 1,
    "account_id": 10,
    "name": "Chemistry 1 H - Blackburn-sem2",
    "enrollment_term_id": 221,
    "uuid": "pNc50PmQxqa1G42hzUFLjgR3JaS4TOGgJ9i0nOko",
    "start_at": "2021-01-04T18:03:47Z",
    "grading_standard_id": null,
    "is_public": false,
    "created_at": "2020-07-22T17:33:31Z",
    "course_code": "Chemistry 1 H - Blackburn",
    "default_view": "modules",
    "license": "private",
    "grade_passback_setting": null,
    "end_at": null,
    "public_syllabus": false,
    "public_syllabus_to_auth": false,
    "storage_quota_mb": 500,
    "is_public_to_auth_users": false,
    "homeroom_course": false,
    "course_color": null,
    "friendly_name": null,
    "apply_assignment_group_weights": false,
    "calendar": {
      "ics": "https://clevelandschools.instructure.com/feeds/calendars/course_pNc50PmQxqa1G42hzUFLjgR3JaS4TOGgJ9i0nOko.ics"
    },
    "time_zone": "America/New_York",
    "blueprint": false,
    "template": false,
    "enrollments": [
      {
        "type": "student",
        "role": "StudentEnrollment",
        "role_id": 3,
        "user_id": 1546,
        "enrollment_state": "active",
        "limit_privileges_to_course_section": false
      }
    ],
    "hide_final_grades": false,
    "workflow_state": "available",
    "restrict_enrollments_to_course_dates": false,
    "overridden_course_visibility": ""
  },
  {
    "id": 10420,
    "root_account_id": 1,
    "account_id": 10,
    "name": "CLIMB 10th Grade - Vacheresse",
    "enrollment_term_id": 352,
    "uuid": "rHFv0zDQQ0MqaLh5ls9Ty2NB65zcL7ZGO8sYhuHk",
    "start_at": "2021-08-02T15:36:49Z",
    "grading_standard_id": null,
    "is_public": null,
    "created_at": "2021-07-19T16:05:24Z",
    "course_code": "CLIMB 10th Grade - Vacheresse",
    "default_view": "feed",
    "license": null,
    "grade_passback_setting": null,
    "end_at": null,
    "public_syllabus": false,
    "public_syllabus_to_auth": false,
    "storage_quota_mb": 500,
    "is_public_to_auth_users": false,
    "homeroom_course": false,
    "course_color": null,
    "friendly_name": null,
    "apply_assignment_group_weights": false,
    "calendar": {
      "ics": "https://clevelandschools.instructure.com/feeds/calendars/course_rHFv0zDQQ0MqaLh5ls9Ty2NB65zcL7ZGO8sYhuHk.ics"
    },
    "time_zone": "America/New_York",
    "blueprint": false,
    "template": false,
    "enrollments": [
      {
        "type": "student",
        "role": "StudentEnrollment",
        "role_id": 3,
        "user_id": 1546,
        "enrollment_state": "active",
        "limit_privileges_to_course_section": false
      }
    ],
    "hide_final_grades": false,
    "workflow_state": "available",
    "restrict_enrollments_to_course_dates": false
  },
  {
    "id": 5378,
    "access_restricted_by_date": true
  },
  {
    "id": 5588,
    "root_account_id": 1,
    "account_id": 10,
    "name": "English 1 H - Semester 2 Colbaugh",
    "enrollment_term_id": 221,
    "uuid": "2KbshUnWT0lFqekusEtVhh5LBLhldQN4OqdoKN5E",
    "start_at": "2021-01-04T12:41:16Z",
    "grading_standard_id": null,
    "is_public": false,
    "created_at": "2020-07-22T17:33:22Z",
    "course_code": "English 1 H-S2 - Colbaugh",
    "default_view": "modules",
    "license": "private",
    "grade_passback_setting": null,
    "end_at": null,
    "public_syllabus": false,
    "public_syllabus_to_auth": false,
    "storage_quota_mb": 500,
    "is_public_to_auth_users": false,
    "homeroom_course": false,
    "course_color": null,
    "friendly_name": null,
    "apply_assignment_group_weights": false,
    "calendar": {
      "ics": "https://clevelandschools.instructure.com/feeds/calendars/course_2KbshUnWT0lFqekusEtVhh5LBLhldQN4OqdoKN5E.ics"
    },
    "time_zone": "America/New_York",
    "blueprint": false,
    "template": false,
    "enrollments": [
      {
        "type": "student",
        "role": "StudentEnrollment",
        "role_id": 3,
        "user_id": 1546,
        "enrollment_state": "active",
        "limit_privileges_to_course_section": false
      }
    ],
    "hide_final_grades": false,
    "workflow_state": "available",
    "restrict_enrollments_to_course_dates": false,
    "overridden_course_visibility": ""
  },
  {
    "id": 10525,
    "root_account_id": 1,
    "account_id": 10,
    "name": "English 2 H - Poulakis",
    "enrollment_term_id": 353,
    "uuid": "U2BRAMor6PFOP2h8LUMdlnVFbtMbSFkHBVGZgS2w",
    "start_at": "2021-08-02T18:52:26Z",
    "grading_standard_id": null,
    "is_public": false,
    "created_at": "2021-07-19T16:05:27Z",
    "course_code": "English 2 H - Poulakis",
    "default_view": "modules",
    "license": "private",
    "grade_passback_setting": null,
    "end_at": null,
    "public_syllabus": false,
    "public_syllabus_to_auth": false,
    "storage_quota_mb": 500,
    "is_public_to_auth_users": false,
    "homeroom_course": false,
    "course_color": null,
    "friendly_name": null,
    "apply_assignment_group_weights": false,
    "calendar": {
      "ics": "https://clevelandschools.instructure.com/feeds/calendars/course_U2BRAMor6PFOP2h8LUMdlnVFbtMbSFkHBVGZgS2w.ics"
    },
    "time_zone": "America/New_York",
    "blueprint": false,
    "template": false,
    "enrollments": [
      {
        "type": "student",
        "role": "StudentEnrollment",
        "role_id": 3,
        "user_id": 1546,
        "enrollment_state": "active",
        "limit_privileges_to_course_section": false
      }
    ],
    "hide_final_grades": false,
    "workflow_state": "available",
    "restrict_enrollments_to_course_dates": false,
    "overridden_course_visibility": ""
  },
  {
    "id": 10708,
    "root_account_id": 1,
    "account_id": 10,
    "name": "Fall World History",
    "enrollment_term_id": 353,
    "uuid": "yGqeV55bAwYP9rQyjmGGE3QO1dYm6c2pGCT9eUQC",
    "start_at": "2021-08-03T12:07:47Z",
    "grading_standard_id": null,
    "is_public": false,
    "created_at": "2021-07-19T16:05:32Z",
    "course_code": "World History and Geography - LaMagna",
    "default_view": "modules",
    "license": "private",
    "grade_passback_setting": "nightly_sync",
    "end_at": null,
    "public_syllabus": false,
    "public_syllabus_to_auth": false,
    "storage_quota_mb": 500,
    "is_public_to_auth_users": false,
    "homeroom_course": false,
    "course_color": null,
    "friendly_name": null,
    "apply_assignment_group_weights": false,
    "calendar": {
      "ics": "https://clevelandschools.instructure.com/feeds/calendars/course_yGqeV55bAwYP9rQyjmGGE3QO1dYm6c2pGCT9eUQC.ics"
    },
    "time_zone": "America/New_York",
    "blueprint": false,
    "template": false,
    "enrollments": [
      {
        "type": "student",
        "role": "StudentEnrollment",
        "role_id": 3,
        "user_id": 1546,
        "enrollment_state": "active",
        "limit_privileges_to_course_section": false
      },
      {
        "type": "student",
        "role": "StudentEnrollment",
        "role_id": 3,
        "user_id": 1546,
        "enrollment_state": "active",
        "limit_privileges_to_course_section": false
      }
    ],
    "hide_final_grades": false,
    "workflow_state": "available",
    "restrict_enrollments_to_course_dates": false,
    "overridden_course_visibility": ""
  },
  {
    "id": 5480,
    "root_account_id": 1,
    "account_id": 10,
    "name": "Geometry H - Storey",
    "enrollment_term_id": 224,
    "uuid": "0hUvYrHEV8wGWFbtqejTMVDMIQtew6lVLZBKl3py",
    "start_at": "2020-08-11T14:06:28Z",
    "grading_standard_id": null,
    "is_public": null,
    "created_at": "2020-07-22T17:33:20Z",
    "course_code": "Geometry H - Storey",
    "default_view": "modules",
    "license": null,
    "grade_passback_setting": null,
    "end_at": null,
    "public_syllabus": false,
    "public_syllabus_to_auth": false,
    "storage_quota_mb": 500,
    "is_public_to_auth_users": false,
    "homeroom_course": false,
    "course_color": null,
    "friendly_name": null,
    "apply_assignment_group_weights": false,
    "calendar": {
      "ics": "https://clevelandschools.instructure.com/feeds/calendars/course_0hUvYrHEV8wGWFbtqejTMVDMIQtew6lVLZBKl3py.ics"
    },
    "time_zone": "America/New_York",
    "blueprint": false,
    "template": false,
    "enrollments": [
      {
        "type": "student",
        "role": "StudentEnrollment",
        "role_id": 3,
        "user_id": 1546,
        "enrollment_state": "active",
        "limit_privileges_to_course_section": false
      }
    ],
    "hide_final_grades": false,
    "workflow_state": "available",
    "restrict_enrollments_to_course_dates": false
  },
  {
    "id": 5347,
    "root_account_id": 1,
    "account_id": 10,
    "name": "Human Geography AP - Brose",
    "enrollment_term_id": 219,
    "uuid": "BNUgjVCnf3SEaa5SEn14RdNXKJAy0RcSjoKbvO7o",
    "start_at": "2020-08-10T01:58:54Z",
    "grading_standard_id": null,
    "is_public": null,
    "created_at": "2020-07-22T17:33:18Z",
    "course_code": "Human Geography AP - Brose",
    "default_view": "modules",
    "license": null,
    "grade_passback_setting": null,
    "end_at": null,
    "public_syllabus": false,
    "public_syllabus_to_auth": false,
    "storage_quota_mb": 500,
    "is_public_to_auth_users": false,
    "homeroom_course": false,
    "course_color": null,
    "friendly_name": null,
    "apply_assignment_group_weights": false,
    "calendar": {
      "ics": "https://clevelandschools.instructure.com/feeds/calendars/course_BNUgjVCnf3SEaa5SEn14RdNXKJAy0RcSjoKbvO7o.ics"
    },
    "time_zone": "America/New_York",
    "blueprint": false,
    "template": false,
    "enrollments": [
      {
        "type": "student",
        "role": "StudentEnrollment",
        "role_id": 3,
        "user_id": 1546,
        "enrollment_state": "active",
        "limit_privileges_to_course_section": false
      }
    ],
    "hide_final_grades": false,
    "workflow_state": "available",
    "restrict_enrollments_to_course_dates": false,
    "overridden_course_visibility": ""
  }
]

For the key with ID 5378, it has no "name" attribute, and I get the error:

KeyError: 'name'

CodePudding user response:

Use .get() to specify a default if it doesn't exist:

print(f'Name: {k.get("name","<na>")}')

Standalone example:

import json

text = '''\
[
    {
        "id": 5378,
        "access_restricted_by_date": true
    },
    {
        "id": 9999,
        "name": "whatever",
        "access_restricted_by_date": true
    }
]
'''

data = json.loads(text)

for item in data:
    print(item['id'], item.get('name','-na-'))

Output:

5378 -na-
9999 whatever
  • Related