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.

Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.

When we hit up this request, when we made this request, we got back a response. The response has things like 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 gonna 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, very similar to Python's as well. So to take that string, we can take that string and we can convert it to actual Python data, to not just a string, but to not a string of a list or a string of a dictionary, but an actual dictionary or list. So 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 gonna be actual data. And the difference here is, if I tried to do, if I tried to access this JSON things 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.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

Right, so we don't want generally to stick with just a string. We wanna 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 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, okay. And let's print that out just for, so that sticks around.

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 Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram