How can we check if a JSON object is empty or not in Java?n

A JSON is a lightweight data-interchange format and the format of JSON is a key with value pair. The JSONObject can parse a text from a String to produce a map-like object and supports java.util.Map interface

We can check whether the JSON object is empty or not in the below example


import java.util.*;
import org.json.*;
public class JSONObjectTest {
   public static void main(String[] args) {
      JSONObject jsonObj = new JSONObject(
         "{" +
            "Name : Jai," +
            "Age : 25, " +
            "Salary: 25000.00 " +
      if(jsonObj.isEmpty()) {
         System.out.println("JSON is empty");
      } else {
         System.out.println("JSON is not empty");


JSON is not empty