Check If Python Json Object is Empty
Last Updated :
25 Apr, 2024
Python users frequently work with JSON, particularly when exchanging data between multiple devices. In this article, we'll learn how to check if a JSON object is empty using Python.
Check If a JSON Object Is Empty in Python
Below, we provide examples to illustrate how to check if a JSON object is empty or not using Python.
Check If a JSON Object Is Empty Using len() Method
In this example, below code defines a function `is_json_empty` that takes a JSON object as input and returns `True` if its length is 0, indicating an empty JSON object. It then creates an empty JSON object, `json_obj`, and prints the result of calling the function.
Python3
import json
def is_json_empty(json_obj):
# return true if length is 0.
return len(json_obj) == 0
json_obj = {} # Empty JSON object
print(is_json_empty(json_obj))
Check If a JSON Object Is Empty Using not Operator
In this example, below code checks if two JSON objects, `Geeks` (empty) and `GeeksTutorial` (with data), are empty and prints the corresponding messages. In this instance, it prints "JSON object 1 is empty." for `Geeks` and "JSON object 2 is not empty." along with the contents of `GeeksTutorial`.
Python3
# JSON object 1
Geeks = {}
# JSON object 2
GeeksTutorial = {
"title": "Introduction to Python",
"author": "GeeksforGeeks",
"topics": ["Basics", "Data Structures", "Functions", "Modules"],
}
# Check if the JSON object is empty
if not Geeks:
print("JSON object 1 is empty.")
else:
print("JSON object 1 is not empty.")
# Check if the JSON object is empty
if not GeeksTutorial:
print("JSON object 2 is empty.")
else:
print("JSON object 2 is not empty.")
print(GeeksTutorial)
OutputJSON object 1 is empty.
JSON object 2 is not empty.
{'title': 'Introduction to Python', 'author': 'GeeksforGeeks', 'topics': ['Basics', 'Data Structures', 'Functions', 'Modules']}
Check If a JSON Object Is Empty Using Comparision
In this example, code defines a function `is_json_empty` that checks if a JSON object is empty by comparing it to an empty dictionary (`{}`). It then creates an empty JSON object, `json_obj`, and prints the result of calling the function with this object, confirming that the output is `True`.
Python3
import json
def is_json_empty(json_obj):
return json_obj == {}
json_obj = {} # Empty JSON object
print(is_json_empty(json_obj))
Conclusion
In this article, we explored three different methods to check if a JSON object is empty in Python. Whether you prefer using the len() function, the not operator, or directly comparing the JSON object with an empty dictionary, you have multiple options to accomplish this task efficiently.
Similar Reads
Check if String is Empty or Not - Python We are given a string and our task is to check whether it is empty or not. For example, if the input is "", it should return True (indicating it's empty), and if the input is "hello", it should return False. Let's explore different methods of doing it with example:Using Comparison Operator(==)The si
2 min read
Python Check if Nonetype or Empty In Python, it's common to check whether a variable is of NoneType or if a container, such as a list or string, is empty. Proper handling of such scenarios is crucial for writing robust and error-free code. In this article, we will explore various methods to check if a variable is either of NoneType
3 min read
Check If a Python Set is Empty In Python, sets are versatile data structures used to store unique elements. It's common to need to check whether a set is empty in various programming scenariosPython# Initializing an empty set s = set() print(bool(s)) # False since the set is empty print(not bool(s)) # True since the set is empty
2 min read
Build a Json Object in Python JSON (JavaScript Object Notation) is a lightweight data-interchange format widely used for data exchange between a server and a web application, as well as between different components of a system. In Python, working with JSON is straightforward, and the built-in json module provides functions to en
2 min read
Check If Value Exists in Python List of Objects When working with Python, we might need to check whether a specific value exists in a list of objects. This problem often arises when dealing with data stored as objects and we want to verify the presence of a specific property value. Using any() Function with List Comprehensionany() function is an
4 min read