0Pricing
Flask Academy · درس

تحويل الكائنات إلى JSON

حوّل العناصر المفردة والمجموعات إلى تسلسل.

تحويل الكائنات إلى JSON درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

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.

الأسئلة الشائعة

هل درس «تحويل الكائنات إلى JSON» مجاني؟

نعم — نص درس «تحويل الكائنات إلى JSON» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.

ماذا ستتعلم في «تحويل الكائنات إلى JSON»؟

حوّل العناصر المفردة والمجموعات إلى تسلسل. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟

لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «تحويل الكائنات إلى JSON»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟

نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. لماذا يفشل بناء JSON يدويًا
  2. تعريف مخطط لنموذج
  3. تحويل الكائنات إلى JSON
  4. تحميل حمولات الإدخال والتحقق منها
← العودة إلى Flask Academy