APIs and JSON

APIs and JSON are essential concepts in modern web development.
They allow different applications and systems to communicate with each other efficiently.

If you use mobile apps, websites, or online services — APIs are working behind the scenes.

What is an API?

API stands for Application Programming Interface.

An API is a set of rules that allows one application to communicate with another.

Think of an API like a waiter in a restaurant:

  • You (client) place an order
  • The waiter (API) takes the request to the kitchen (server)
  • The kitchen prepares the food (processes request)
  • The waiter brings the response back to you

How APIs Work

  1. Client sends a request to the API
  2. API forwards request to the server
  3. Server processes the request
  4. Server sends response back through API
  5. Client receives data

This usually happens using HTTP.

Common HTTP Methods Used in APIs

GET → Retrieve data
POST → Send new data
PUT → Update existing data
DELETE → Remove data

Example:

GET /users → Get list of users
POST /users → Create new user

What is JSON?

JSON stands for JavaScript Object Notation.

It is a lightweight data format used to send and receive data between client and server.

JSON is:

  • Easy to read
  • Easy to write
  • Language-independent
  • Widely used in APIs

Example of JSON Data

{
"name": "Ali",
"age": 25,
"city": "Karachi"
}

JSON structure uses:

  • Curly braces {}
  • Key-value pairs
  • Strings in double quotes
  • Commas to separate items

JSON Array Example

[
{
"name": "Ali",
"age": 25
},
{
"name": "Sara",
"age": 28
}
]

This represents multiple objects.

API Response Example

When you call an API, it may return:

{
"status": "success",
"data": {
"id": 1,
"name": "Ayesha",
"email": "ayesha@example.com"
}
}

The frontend reads this JSON and displays the data.

Why APIs and JSON Are Important

They allow:

Mobile apps to communicate with servers
Websites to fetch live data
Systems to integrate with other systems
Data exchange between different technologies

For example:

  • Payment gateways use APIs
  • Social media apps use APIs
  • Weather apps fetch data using APIs

What is a REST API?

REST (Representational State Transfer) is a common style for building APIs.

REST APIs:

  • Use HTTP methods
  • Use URLs for resources
  • Return data in JSON format

Example:

GET /products
GET /products/1
POST /products

Key Takeaway

APIs allow applications to communicate with each other.
JSON is the format used to exchange data in a simple and structured way.

Together, APIs and JSON form the foundation of modern web and mobile applications.

Home » PYTHON FOR WEB DEVELOPMENT (PYWEB) > Web Basics > APIs and JSON