
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a JSON Object is Empty in Java
In this article, let's learn how to check if a JSON object has any values or not in Java. If you don't know about JSON, refer JSON.
We will use the org.json library and its methods like isEmpty()
, length()
, and keys()
to check if a JSON object is empty or not. We can check if a JSON object is empty or not using the following methods:
- Using isEmpty() method
- Using length() method
- Using keys() method
Let's see how to use each of them one by one.
Using isEmpty() method
We can use the isEmpty() method to check if a JSON object is empty or not. It returns true if the JSON object is empty, otherwise, it returns false. Let's see the code to check if a JSON object is empty or not using the isEmpty() method:
import org.json.JSONObject; import org.json.JSONException; public class CheckEmptyJsonObjectExample { public static void main(String[] args) throws JSONException { // Create a JSON object JSONObject jsonObj = new JSONObject("{}"); // Check if the JSON object is empty or not if (jsonObj.isEmpty()) { System.out.println("JSON object is empty."); } else { System.out.println("JSON object is not empty."); } } }
Output
Following is the output of the above code:
JSON object is empty.
Using length() method
We can use the length() method to check if a JSON object is empty or not. It returns the number of keys in the JSON object. If it returns 0, then the JSON object is empty. Let's see the code to check if a JSON object is empty or not using the length() method:
import org.json.JSONObject; import org.json.JSONException; public class CheckEmptyJsonObjectExample { public static void main(String[] args) throws JSONException { // Create a JSON object JSONObject jsonObj = new JSONObject("{}"); // Check if the JSON object is empty or not if (jsonObj.length() == 0) { System.out.println("JSON object is empty."); } else { System.out.println("JSON object is not empty."); } } }
Following is the output of the above code:
JSON object is empty.
Using keys() method
Another way is the keys() method to check if a JSON object is empty or not. It returns an iterator of the keys in the JSON object. If it returns an empty iterator, then the JSON object is empty. Let's see the code to check if a JSON object is empty or not using the keys() method:
import org.json.JSONObject; import org.json.JSONException; import java.util.Iterator; public class CheckEmptyJsonObjectExample { public static void main(String[] args) throws JSONException { // Create a JSON object JSONObject jsonObj = new JSONObject("{}"); // Check if the JSON object is empty or not Iterator<String> keys = jsonObj.keys(); if (!keys.hasNext()) { System.out.println("JSON object is empty."); } else { System.out.println("JSON object is not empty."); } } }
Following is the output of the above code:
JSON object is empty.
Need to verify if JSON is empty or not in Java?
Checking if the object is empty or not is a very simple task. We need to do this in situations like:
- When we need to validate the JSON object.
- Before using the JSON data for applications, we might want to verify whether it has data or not.
- To handle or avoid errors, we check before using the JSON object.
- To skip processing if there's no data in the JSON object.
- To make the program faster by ignoring empty objects.