#C14568. Organize Students by Grade
Organize Students by Grade
Organize Students by Grade
You are given a JSON object representing student data. The JSON object contains a key students which maps to an array of student records. Each student record is a JSON object with possible keys: name, age, grade, and favorite_subject. Your task is to group the student records by their grade level. For every student record in the output, include the keys name, age, and favorite_subject. If any of these keys are missing in the input, output them as null
.
Formally, if a student record s is provided, let ( name = s.name ), ( age = s.age ), and ( favorite_subject = s.favorite_subject ), with any missing field taken as ( null ). Only records with a valid grade field should be processed; records without a grade key are ignored.
inputFormat
The input is a JSON string read from standard input. It is a JSON object with a key "students" that maps to an array of student records. Each record may include the keys: name (string), age (number), grade (string), and favorite_subject (string).
outputFormat
Print a JSON string to standard output. The output is a JSON object where each key is a grade level (as a string) and the corresponding value is an array of student records. Each record must include the keys: name, age, and favorite_subject, with missing keys represented as null.## sample
{"students": [{"name": "Alice", "age": 14, "grade": "8", "favorite_subject": "Math"}, {"name": "Bob", "age": 13, "grade": "7", "favorite_subject": "Science"}, {"name": "Charlie", "age": 14, "grade": "8"}, {"name": "David", "age": 15, "grade": "9", "favorite_subject": "History"}, {"name": "Eve", "age": 13, "grade": "7", "favorite_subject": "Art"}]}
{"8": [{"name": "Alice", "age": 14, "favorite_subject": "Math"}, {"name": "Charlie", "age": 14, "favorite_subject": null}], "7": [{"name": "Bob", "age": 13, "favorite_subject": "Science"}, {"name": "Eve", "age": 13, "favorite_subject": "Art"}], "9": [{"name": "David", "age": 15, "favorite_subject": "History"}]}