DEV Community

Cover image for APIs and JSON Handling in Python – Day 22
augustineowino357-design
augustineowino357-design

Posted on

APIs and JSON Handling in Python – Day 22

In the previous lesson, we learned about Web Development using Flask and how Python can be used to build websites and web applications. Today, we will learn about APIs and JSON Handling, which are essential in modern software development.

APIs allow applications to communicate with each other and exchange data over the internet.


What is an API?

API stands for Application Programming Interface.

An API allows one application to communicate with another application.

APIs are used in:

  • Mobile applications
  • Websites
  • Payment systems
  • Weather applications
  • Social media platforms

For example:

  • A weather app gets weather data from a weather API
  • A payment app connects to banking APIs
  • Social media apps use APIs for sharing data

Why APIs are Important

APIs help developers:

  • Access external services
  • Share data between applications
  • Automate tasks
  • Build powerful applications
  • Integrate different systems

Most modern applications rely heavily on APIs.


What is JSON?

JSON stands for JavaScript Object Notation.

JSON is a lightweight format used for storing and exchanging data.

JSON is easy:

  • To read
  • To write
  • For humans
  • For machines

Example of JSON Data

{
"name": "Augustine",
"age": 21,
"course": "Python"
}


Python and JSON

Python provides the built-in "json" module for handling JSON data.

Example

import json


Converting Python Dictionary to JSON

The "json.dumps()" method converts Python objects into JSON format.

Example

import json

student = {
"name": "Augustine",
"age": 21
}

json_data = json.dumps(student)

print(json_data)

Output

{"name": "Augustine", "age": 21}


Converting JSON to Python Dictionary

The "json.loads()" method converts JSON data into a Python dictionary.

Example

import json

data = '{"name":"Augustine","age":21}'

python_data = json.loads(data)

print(python_data)

Output

{'name': 'Augustine', 'age': 21}


Installing the requests Library

The "requests" library helps Python interact with APIs.

Example

pip install requests


Importing requests

Example

import requests


Sending a GET Request

A GET request retrieves data from an API.

Example

import requests

response = requests.get("https://api.github.com")

print(response.status_code)

Output

200

A status code of "200" means the request was successful.


Viewing API Response Data

Example

import requests

response = requests.get("https://api.github.com")

print(response.text)

This displays the API response.


Converting API Response to JSON

Example

import requests

response = requests.get("https://api.github.com")

data = response.json()

print(data)


Accessing JSON Data

Example

import requests

response = requests.get("https://api.github.com")

data = response.json()

print(data["current_user_url"])


Sending POST Requests

A POST request sends data to an API.

Example

import requests

data = {
"username": "Augustine",
"password": "1234"
}

response = requests.post(
"https://example.com/api",
json=data
)

print(response.status_code)


HTTP Status Codes

Status Code| Meaning
"200"| Success
"201"| Created
"400"| Bad Request
"401"| Unauthorized
"404"| Not Found
"500"| Server Error


API Authentication

Some APIs require authentication using:

  • API Keys
  • Tokens
  • User credentials

Example

headers = {
"Authorization": "Bearer YOUR_TOKEN"
}


Error Handling with APIs

API requests may fail because of:

  • Internet problems
  • Invalid URLs
  • Server errors

Example

import requests

try:
response = requests.get("https://api.github.com")

print(response.status_code)
Enter fullscreen mode Exit fullscreen mode

except requests.exceptions.RequestException:
print("Request failed")


Real-World Applications of APIs

APIs are used in:

  • Weather Applications
  • Mobile Banking
  • Payment Gateways
  • Social Media Platforms
  • GPS Systems
  • Online Shopping Platforms

Advantages of APIs

APIs help applications:

  • Share data easily
  • Integrate services
  • Automate systems
  • Improve scalability
  • Build modern cloud applications

Most modern software systems depend on APIs.


Conclusion

APIs and JSON are essential technologies in modern Python development because they allow applications to communicate and exchange data efficiently.

Understanding how to work with APIs helps developers build powerful real-world applications such as web apps, mobile apps, and cloud systems.

Practice sending API requests and working with JSON data to strengthen your Python development skills.

Python #PythonForBeginners #CodingJourney #LearnPython #Programming

Top comments (0)