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


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

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");
      }
   }
}

Output

JSON is not empty

Updated on: 04-Jul-2020

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements