Check Whether a String is Valid JSON in Java

06 May 2023 Balmiki Mandal 0 Core Java

Check Whether a String is Valid JSON in Java

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write, and easy for machines to parse and generate. JSON is commonly used for asynchronous communication in web applications, and for data storage and interchange in mobile applications.

In Java, there are two ways to check whether a string is valid JSON:

1. Using the JSONObject class. The JSONObject class is part of the org.json library, which is a popular JSON parser for Java. To check whether a string is valid JSON using the JSONObject class, you can use the following code:

Java
import org.json.JSONObject;

public class JSONValidator {

    public static boolean isValid(String json) {
        try {
            new JSONObject(json);
        } catch (JSONException e) {
            return false;
        }
        return true;
    }
}

To use the JSONValidator class, you can simply call the isValid() method with the JSON string that you want to validate. The isValid() method will return true if the JSON string is valid, and false otherwise.

2. Using the JSONParserFactory class. The JSONParserFactory class is part of the javax.json library, which is a newer JSON parser for Java. To check whether a string is valid JSON using the JSONParserFactory class, you can use the following code:

Java
import javax.json.JsonParserFactory;

public class JSONValidator {

    public static boolean isValid(String json) {
        JsonParserFactory parserFactory = JsonParserFactory.newInstance();
        try {
            parserFactory.createParser(json).close();
        } catch (JsonException e) {
            return false;
        }
        return true;
    }
}

To use the JSONValidator class, you can simply call the isValid() method with the JSON string that you want to validate. The isValid() method will return true if the JSON string is valid, and false otherwise.

Example usage:

Java
String json = "{\"name\": \"John Doe\", \"age\": 30}";

// Check whether the JSON string is valid using the JSONObject class.
boolean isValid = JSONValidator.isValid(json, JSONObject.class);

// Check whether the JSON string is valid using the JSONParserFactory class.
isValid = JSONValidator.isValid(json, JsonParserFactory.class);

If you are unsure which JSON parser to use, I recommend using the JSONParserFactory class. The JSONParserFactory class is a newer parser that is supported by the Java EE platform.

Top Resources Now:


Enroll Now:

 

[Course in production] "Start Supercharging Your Productivity!"

Contact Us:

 

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.