Hackforge Academy

Category: python

returning json responses rest apis

Published on 30 Sep 2026

Explanation

Flask can return dictionaries and lists directly as JSON responses.

Code:

@app.route('/api/users')
def users():
    return {
        'data': [
            {'id': 1, 'name': 'Arun'},
            {'id': 2, 'name': 'Kumar'}
        ]
    }

Explanation

jsonify() can explicitly create a JSON response with the correct content type.

Code:

from flask import jsonify

@app.route('/api/user')
def user():
    return jsonify({
        'id': 101,
        'name': 'Praveen'
    })

Explanation

POST APIs can read JSON request bodies using request.get_json().

Code:

@app.route('/api/users', methods=['POST'])
def create_user():
    data = request.get_json()
    return jsonify(data), 201

Explanation

REST APIs commonly use resource-oriented URLs and HTTP methods.

Code:

GET    /api/users
GET    /api/users/101
POST   /api/users
PUT    /api/users/101
DELETE /api/users/101

Explanation

A REST API should return meaningful HTTP status codes along with JSON responses.

Code:

200 → Success
201 → Created
400 → Bad Request
401 → Unauthorized
404 → Not Found
500 → Server Error

🚀 Learn Spring Boot with real-world projects

💡 Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
⚛️

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
☕

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community