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 Fundamentals
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
Free Preview Lesson
Devops Lab | 60
Python Basics • 28min
0 / 7 lessons complete
Section Overview
Free Preview Lesson
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!
Free Preview Lesson
Text | 5 min
Python Operators
Free Preview Lesson
Text | 4 min
Section Review
Free Preview Lesson
Quiz | 5 min
Python Variables • 41min
0 / 8 lessons complete
Section Overview
Free Preview Lesson
Text | 4 min
Casting
Free Preview Lesson
Text | 4 min
Strings
Free Preview Lesson
Text | 5 min
Booleans
Free Preview Lesson
Text | 8 min
User Input
Free Preview Lesson
Text | 2 min
Numbers
Free Preview Lesson
Text | 7 min
NoneType
Free Preview Lesson
Text | 5 min
Assignment: Write a Mad Libs Script
Free Preview Lesson
Text | 6 min
Even more Python Variables! • 41min
0 / 6 lessons complete
Python Lists
Free Preview Lesson
Text | 8 min
Python Tuples
Free Preview Lesson
Text | 7 min
Python Sets
Free Preview Lesson
Text | 7 min
Frozensets
Free Preview Lesson
Text | 6 min
Dictionaries
Free Preview Lesson
Text | 8 min
Iterator and Iterable
Free Preview Lesson
Text | 5 min
Conditional Statements • 15min
0 / 3 lessons complete
Section Overview
Free Preview Lesson
Text | 2 min
If Statements
Free Preview Lesson
Text | 6 min
Try, Except
Free Preview Lesson
Text | 7 min
Writing Functions • 30min
0 / 5 lessons complete
Section Overview
Free Preview Lesson
Text | 3 min
Defining Functions
Free Preview Lesson
Text | 4 min
Importing functions from another file
Free Preview Lesson
Text | 4 min
Assignment: Rock, paper, scissors
Free Preview Lesson
Text | 12 min
Assignment: Count Dictionary Words
Free Preview Lesson
Text | 7 min
Python Loops • 23min
0 / 5 lessons complete
Section Overview
Free Preview Lesson
Text | 2 min
For In Loops
Free Preview Lesson
Text | 5 min
While Loops
Free Preview Lesson
Text | 5 min
Nested Loops
Free Preview Lesson
Text | 3 min
Python Loops Challenge!
Free Preview Lesson
Text | 8 min
Python PIP and Modules • 18min
0 / 4 lessons complete
Section Overview
Free Preview Lesson
Text | 3 min
Installing Python PIP
Free Preview Lesson
Text | 4 min
Installing Modules with PIP
Free Preview Lesson
Text | 5 min
Importing Modules
Free Preview Lesson
Text | 6 min
RegEx • 26min
0 / 4 lessons complete
Section Overview
Free Preview Lesson
Text | 4 min
Regex 101
Free Preview Lesson
Text | 10 min
Importing Regex and manipulating strings
Free Preview Lesson
Text | 7 min
Regex Challenge!
Free Preview Lesson
Text | 5 min
Working with APIs • 12min
0 / 3 lessons complete
Course Conclusion • 2min
0 / 1 lessons complete