How to return a json object from a Python function?
Last Updated :
28 May, 2025
Returning a JSON object from a Python function involves converting Python data (like dictionaries or lists) into a JSON-formatted string or response, depending on the use case. For example, if you're working with APIs, you might return a JSON response using frameworks like Flask. Let's explore several efficient methods to return a JSON object from a Python function.
Using json.dumps()
This is the most basic and widely-used way to convert a Python dictionary to a JSON string using Python’s built-in json module. It doesn't require any third-party packages or frameworks.
Python
import json
def fun():
d = {
"language": "Python",
"company": "GeeksForGeeks",
"Itemid": 1,
"price": 0.00
}
return json.dumps(d)
print(fun())
Output{"language": "Python", "company": "GeeksForGeeks", "Itemid": 1, "price": 0.0}
Explanation: fun() function creates a dictionary with details like language, company, item ID and price, then uses json.dumps() to convert it into a JSON string. print(fun()) calls the function and prints the JSON output to the console.
Using orjson
orjson is a high-performance JSON library for Python. It is faster and more efficient than the standard json module, especially when working with large data or requiring faster serialization.
Python
import orjson
def fun():
d = {
"language": "Python",
"company": "GeeksForGeeks",
"Itemid": 1,
"price": 0.00
}
return orjson.dumps(d).decode()
print(fun())
Output
{"language":"Python","company":"GeeksForGeeks","Itemid":1,"price":0.0}
Explanation: fun() function creates a dictionary with details like language, company, item ID and price, then uses orjson.dumps() to convert it into a JSON string. Since orjson returns bytes, .decode() is used to convert it to a string.
Using jsonify()
jsonify() is part of Flask and is used to create a proper HTTP JSON response. It is the best choice when you're building a web API or returning JSON data from a Flask route.
Python
from flask import Flask, jsonify
app = Flask(__name__)
def fun():
d = {"language": "Python"}
return jsonify(d)
with app.app_context():
response = fun()
print(response.get_data(as_text=True))
Output
{"language":"Python"}
Explanation: fun() function creates a dictionary and uses jsonify() to convert it into a JSON response. Since jsonify() requires a Flask context, app.app_context() is used. The response is then printed using response.get_data(as_text=True).
Similar Reads
Returning a function from a function - Python In Python, functions are first-class objects, allowing them to be assigned to variables, passed as arguments and returned from other functions. This enables higher-order functions, closures and dynamic behavior.Example:Pythondef fun1(name): def fun2(): return f"Hello, {name}!" return fun2 # Get the
5 min read
Return Dictionary from a Function in Python Returning a dictionary from a function allows us to bundle multiple related data elements and pass them back easily. In this article, we will explore different ways to return dictionaries from functions in Python.The simplest approach to returning a dictionary from a function is to construct it dire
3 min read
How a Function Returns an Object in JavaScript ? JavaScript Functions are versatile constructs that can return various values, including objects. Returning objects from functions is a common practice, especially when you want to encapsulate data and behavior into a single entity. In this article, we will see how a function returns an object in Jav
3 min read
How to call a function in Python Python is an object-oriented language and it uses functions to reduce the repetition of the code. In this article, we will get to know what are parts, How to Create processes, and how to call them.In Python, there is a reserved keyword "def" which we use to define a function in Python, and after "de
5 min read
How to return a JSON response from a Flask API ? Flask is one of the most widely used python micro-frameworks to design a REST API. In this article, we are going to learn how to create a simple REST API that returns a simple JSON object, with the help of a flask. Prerequisites: Introduction to REST API What is a REST API? REST stands for Represent
3 min read
How to read a JSON response from a link in Python? There is a huge amount of data available on the web and most of them are in form of (JavaScript Object Notation) JSON. But it is difficult for humans to directly read and use it. To resolve this problem in python we have different libraries which help us to read the JSON data fetched from the web. T
2 min read