Objekte in JSON umwandeln
Serialisieren Sie einzelne Elemente und Sammlungen.
Objekte in JSON umwandeln ist eine kostenlose Flask Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Flask Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Flask Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Dumping Defined
Turning a model object into plain JSON-ready data is called dumping. The schema reads your object and emits a clean dict.
Call dump
Pass your object to the schema's dump method. It returns a dictionary containing only the fields you declared, properly typed.
result = user_schema.dump(user)A Plain Dict Comes Back
The output of dump is an ordinary Python dict, not JSON text yet. That dict is exactly what jsonify happily turns into a response.
Pair with jsonify
Hand the dumped dict to Flask's jsonify to get a real response with the right content type and proper formatting.
return jsonify(user_schema.dump(user))Serialize Many at Once
Have a list of objects? Create the schema with many=True and dump returns a list of dicts in one tidy call.
UserSchema(many=True).dump(users)Reuse with the many Flag
You can also pass many=True straight to dump on an existing instance, so one schema serves both single and list endpoints.
user_schema.dump(users, many=True)Nested Objects Flow Through
Declare a Nested field and dump walks into related objects too. One call serializes a user and all of their posts together.
posts = fields.Nested(PostSchema, many=True)Dates Format Themselves
Because you used a DateTime field, dump renders it as an ISO string with no extra work. Consistent timestamps come for free.
Only Declared Fields Appear
Dump ignores anything not in the schema. Internal attributes like password_hash simply never show up, so your output stays safe.
Dump Never Validates
Remember that dump trusts your objects and does no validation. It is purely about output; checking input is the job of load.
A Complete View
Put it together: query the model, dump it, jsonify it, return it. That short pipeline replaces all those fragile hand-built dicts.
return jsonify(schema.dump(query_result))Quick Check
You need to serialize a list of user objects.
Recap
Use dump to turn objects into JSON-ready dicts, add many=True for lists, and let Nested fields handle relations. Then jsonify and return.
Häufig gestellte Fragen
Ist die Lektion „Objekte in JSON umwandeln“ kostenlos?
Ja — der vollständige Text von „Objekte in JSON umwandeln“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Flask Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Flask Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Objekte in JSON umwandeln“?
Serialisieren Sie einzelne Elemente und Sammlungen. Du übst Flask Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Flask Academy zu starten?
Keine Vorkenntnisse erforderlich. Flask Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „Objekte in JSON umwandeln“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Flask Academy-Lektion Code schreiben und ausführen?
Ja. Jede Flask Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Warum selbst erstelltes JSON scheitert
- Ein Schema für ein Modell definieren
- Objekte in JSON umwandeln
- Input-Payloads laden und validieren