Getting JSON with Python Requests Library

To request JSON data from the server using the Python Requests library, call the request.get() method and pass the target URL as a first parameter. The Python Requests Library has a built-in JSON decoder and automatically converts JSON strings into a Python dictionary. If JSON decoding fails, then response.json() will throw a "requests.exceptions.JSONDecodeError" exception; for example, when response code 204 (no content) or JSON contains invalid JSON. In this Python GET JSON example, we send a GET request to the ReqBin echo URL and receive JSON data in the response. The custom 'Accept: application/json' header tells the server that the client expects a JSON. Click Execute to run Python Requests GET JSON Example online and see the result.
Getting JSON with Python Requests Library Execute
import requests

headers = {'Accept': 'application/json'}

r = requests.get('https://reqbin.com/echo/get/json', headers=headers)

print(f"Response: {r.json()}")
Updated: Viewed: 18382 times

What is JSON?

JSON (JavaScript Object Notation) is a text-based, language-independent data format for structured storage and data exchange in a human-readable format. JSON is used in nearly every scripting language. JSON is used to exchange data between applications written in many programming languages, including JavaScript, Java, C++, C#, Go, PHP, Python, and more. JSON allows you to transfer information over the network. After receiving the data in JSON format, the client converts the data into objects in the given programming language. JSON is not the only format for storing data; other formats exist, such as XML and YAML.

What is the Python Requests library?

The Requests Library is a popular library for sending HTTP requests in Python. Library, because of its simplicity and ease of use, has become the standard way to send HTTP POST, GET, DELETE requests (and other types), although it is not contained in the Python distribution. The Requests Library is based on the urllib3 library and hides the complexness of making HTTP requests behind a simple API. The Requests Library supports SSL connections, session cookies, and international domain names.

How to use the Python Requests library in your code?

To install the Python Requests library, run the following command:

Install Python Requests Library
pip install requests

After installing the Request Library, you can use it in your work:

import requests

How to request data in JSON format using Python?

Python clients must explicitly notify servers that they expect JSON data by sending Accept: application/json request header. Without the Accept header, the server may send data in a different format.

JSON Accept Header Example
Accept: application/json

Using Python Requests library to fetch JSON from the server

The following is an example of Fetch JSON using the Python Requests Library:

Get JSON using Python Requests Example
import requests

r = requests.get('https://reqbin.com/echo/get/json', 
                 headers={'Accept': 'application/json'})

print(f"Response: {r.json()}")

See also