Converting JSON Strings to Python Data: A Guide to Using Requests

Convert JSON responses into Python dictionaries using the .json() method from the requests library.

Understand how JSON data is transmitted over the internet and seamlessly converted into Python-friendly data types. Learn practical steps for leveraging Python's requests library to handle JSON responses effectively.

Key Insights

  • JSON is commonly used for transmitting data online due to its compatibility with JavaScript syntax and similarity to Python dictionaries and lists, enabling straightforward data interchange.
  • When JSON data is received, it arrives as a string, which must be converted into native Python data structures (e.g., dictionaries or lists) for practical use and analysis.
  • The Python requests library simplifies handling JSON data by providing a built-in .json() method, automatically transforming JSON-formatted string responses into usable Python dictionaries.

This lesson is a preview from our Data Science & AI Certificate Online (includes software) and Python Certification Online (includes software & exam). Enroll in a course for detailed lessons, live instructor support, and project-based training.

"

When we made this request, we got back a response. The response has things like a status code on it. It also has some content on it, and that content is a big string.

And for sending data over the internet, it's typically sent as a string. And in our case, and this is the most common case, it's going to be sent as a JSON string. JSON is a standard way of sending data over the internet, based on JavaScript's syntax for dictionaries and lists, which are called objects and arrays in JavaScript.

But they're very similar to Python's syntax as well. So to take that string, we can take that string and we can convert it into actual Python data—not just a string representation, but an actual dictionary or list. For example, our JSON might be, let's do some fruits like that, like that fruit one before.

Let's say it was just a dictionary of fruits, or a list of fruits rather. So JSON makes this a string, but when we convert it to Python, it's going to be without any quotes; it's going to be actual data. And the difference here is, if I tried to do, if I tried to access this JSON string's index zero, it's a string, so I would get the first character in it.

Right, with a string, your index is just the first letter, symbol, character in the string. Whereas if it's Python, if it's a Python dictionary, and I access the first thing, I would get back the string apple, like so. The actual data that I actually wanted.

Right, so we generally don't want to stick with just a string. We want to convert it to an actual value. And every library, like the requests library in Python, has a way to do that, right? Requests is making this request, giving us back a response object with things like status code on it, things built to work with Python.

And it also has a .json() method, which will convert this response from a JSON string to actual Python. All right, let's set that to be data. And let's check what this data's type is.

It's a dictionary. And let's print that out so it sticks around.

"
photo of Colin Jaffe

Colin Jaffe

Colin Jaffe is a programmer, writer, and teacher with a passion for creative code, customizable computing environments, and simple puns. He loves teaching code, from the fundamentals of algorithmic thinking to the business logic and user flow of application building—he particularly enjoys teaching JavaScript, Python, API design, and front-end frameworks.

Colin has taught code to a diverse group of students since learning to code himself, including young men of color at All-Star Code, elementary school kids at The Coding Space, and marginalized groups at Pursuit. He also works as an instructor for Noble Desktop, where he teaches classes in the Full-Stack Web Development Certificate and the Data Science & AI Certificate.

Colin lives in Brooklyn with his wife, two kids, and many intricate board games.

More articles by Colin Jaffe

How to Learn Data Science

Build practical, career-focused machine learning skills through hands-on training designed for beginners and professionals alike. Learn fundamental tools and workflows that prepare you for real-world projects or industry certification.