Working with JSON

In this lesson you are going to learn more about working with JSON objects that HTTP. Let's start with the following HTTP get request:

import requests

# Make a GET request to the endpoint
response = requests.get("https://jsonplaceholder.typicode.com/todos/1")

# Print the entire response
print(response.json())

The response.json() method is used to convert the response of an HTTP request to a JSON object. This is often useful when working with APIs that return JSON data, as it allows us to easily access and manipulate the data in our Python code.

In the example code you provided, we are making a GET request to the https://jsonplaceholder.typicode.com/todos/1 endpoint using the requests module. This endpoint returns a JSON object representing a to-do item with an ID of 1.

Python JSON vs Dictionary?

The main difference between Python dictionaries and JSON objects is the way they are structured. In Python, a dictionary is a collection of key-value pairs, where the keys must be unique and are usually strings. In JSON, an object is a collection of key-value pairs, where the keys must be strings and the values can be of any data type (including other objects or arrays).

Accessing data inside of the dictionary object

For example, to access the "title" of the to-do item, we can use the following code:

# Print the response
data = response.json() # Parse the JSON into a dictionary named 'data'
title = data["title"] # Extract the title key value
print(title) # Print the value

This will retrieve the value associated with the "title" key, which in this case is "delectus aut autem".

We can also loop through the keys and values of the dictionary to do more complex manipulations of the data. For example:

# Print the response
data = response.json()

# Loop through the keys and values in the dictionary
for key, value in data.items():
    # Print the key and value
    print(f"{key}: {value}")

This will loop through each key and value in the dictionary and print them out.

That's the basics of working with the response.json() method in Python! It's a convenient way to convert the response of an HTTP request to a JSON object and access and manipulate the data in your Python code.

Server Academy Members Only

Want to access this lesson? Just sign up for a free Server Academy account and you'll be on your way. Already have an account? Click the Sign Up Free button to get started..

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments

Saving Progress...

Sign up for free!

Sign up for free and get instant access to this course!.

Python 3 for Beginners

0%

0/1 Lessons

Installing Python on Windows

• 1hr 17min

0 / 4 lessons complete

Python Basics

• 28min

0 / 7 lessons complete

Python Variables

• 41min

0 / 8 lessons complete

Even more Python Variables!

• 41min

0 / 6 lessons complete

Conditional Statements

• 15min

0 / 3 lessons complete

Writing Functions

• 30min

0 / 5 lessons complete

Python Loops

• 23min

0 / 5 lessons complete

Python PIP and Modules

• 18min

0 / 4 lessons complete

RegEx

• 26min

0 / 4 lessons complete

Working with APIs

• 12min

0 / 3 lessons complete

Course Conclusion

• 2min

0 / 1 lessons complete