Working with JSON
Server Academy Members Only
Sorry, this lesson is only available to Server Academy members. Create a free account now to get instant access to this and more free courses. Click the Sign Up Free button below to get access to our free courses now, or sign in if you have an account.
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..
Saving Progress...
Python 3 for Beginners
Installing Python on Windows • 1hr 17min
0 / 4 lessons complete
Section Overview
Free Preview Lesson
Text | 1 min
Downloading and Installing Python on Windows
Free Preview Lesson
Text | 8 min
Installing and configuring VS Code for Python
Free Preview Lesson
Text | 8 min
Lab: Installing Python
Devops Lab | 60
Python Basics • 28min
0 / 7 lessons complete
Section Overview
Text | 2 min
Executing Python Code
Free Preview Lesson
Text | 3 min
Python 3 Syntax
Free Preview Lesson
Text | 5 min
Help! Python Keywords
Free Preview Lesson
Text | 4 min
Printing to the console!
Text | 5 min
Python Operators
Text | 4 min
Section Review
Quiz | 5 min
Python Variables • 41min
0 / 8 lessons complete
Section Overview
Text | 4 min
Casting
Text | 4 min
Strings
Text | 5 min
Booleans
Text | 8 min
User Input
Text | 2 min
Numbers
Text | 7 min
NoneType
Free Preview Lesson
Text | 5 min
Assignment: Write a Mad Libs Script
Text | 6 min
Even more Python Variables! • 41min
0 / 6 lessons complete
Python Lists
Text | 8 min
Python Tuples
Text | 7 min
Python Sets
Text | 7 min
Frozensets
Text | 6 min
Dictionaries
Text | 8 min
Iterator and Iterable
Text | 5 min
Conditional Statements • 15min
0 / 3 lessons complete
Writing Functions • 30min
0 / 5 lessons complete
Section Overview
Text | 3 min
Defining Functions
Text | 4 min
Importing functions from another file
Text | 4 min
Assignment: Rock, paper, scissors
Text | 12 min
Assignment: Count Dictionary Words
Text | 7 min
Python Loops • 23min
0 / 5 lessons complete
Section Overview
Text | 2 min
For In Loops
Text | 5 min
While Loops
Text | 5 min
Nested Loops
Text | 3 min
Python Loops Challenge!
Text | 8 min
Python PIP and Modules • 18min
0 / 4 lessons complete
Section Overview
Text | 3 min
Installing Python PIP
Text | 4 min
Installing Modules with PIP
Text | 5 min
Importing Modules
Text | 6 min
RegEx • 26min
0 / 4 lessons complete
Section Overview
Text | 4 min
Regex 101
Text | 10 min
Importing Regex and manipulating strings
Text | 7 min
Regex Challenge!
Text | 5 min
Working with APIs • 12min
0 / 3 lessons complete
Course Conclusion • 2min
0 / 1 lessons complete