CodingNic

Flask Fundamentals

JSON Responses

Flask Fundamentals 10 min read

JSON Responses

JSON Responses

Web applications don’t always return HTML.

Many applications return data in JSON format, especially when building APIs.

What is a JSON Response?

A JSON response is data returned by the server in JSON format.

It is commonly used for communication between frontend and backend systems.

Returning JSON in Flask

Flask makes it easy to return JSON using jsonify.

id="json-response"
from flask import Flask, jsonify app = Flask(__name__) @app.route("/api") def api(): return jsonify({ "message": "Hello, World!", "status": "success" })

How It Works

  • The client sends a request
  • Flask processes the request
  • Flask returns JSON data
  • The client receives and uses the data

JSON Flow Diagram

Why JSON Matters

JSON is the standard format for APIs.

It allows:

  • Frontend apps to communicate with backend servers
  • Mobile apps to fetch data
  • Systems to exchange structured information

Example Use Cases

JSON responses are used in:

  • APIs
  • Dashboards
  • Single-page applications
  • Mobile applications

JSON vs HTML

  • HTML → used for rendering pages
  • JSON → used for sending data

Modern applications often use both.

Why This Matters

In this course:

  • You will build APIs using Flask and FastAPI
  • You will send and receive JSON data
  • You will connect frontend and backend systems

Challenge

Return JSON data from your app.

What to do

  1. Create a new route /api/user

  2. Inside the function:

    • Return JSON using jsonify()

    • Include:

      • name
      • age
      • status
  3. Create another route /api/products

  4. Return a list of items like:

    • “Book”
    • “Phone”
    • “Laptop”

What you should test

  • Open /api/user in your browser
  • You should see JSON data
  • Open /api/products and see a list

Summary

  • JSON responses return structured data
  • Flask uses jsonify to send JSON
  • JSON is essential for APIs

In the next lesson, you’ll apply everything you’ve learned in a mini project.