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

JavaJSONObject Oriented ProgrammingProgramming

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
raja
Published on 19-Sep-2019 18:24:22
Advertisements