0

I've already said, my question is different...question is already asked here, but i want a predefined method in java which checksgiven string is json format or not.

if there is no predefined method then at least tell a code which checks JSON Format or not, without using try catch block..

Thanks in advance...

Community
  • 1
  • 1
sai
  • 419
  • 2
  • 7
  • 17
  • What's wrong with `try-catch`? – cassiomolin Jan 25 '17 at 11:19
  • 1
    It can easily be done using `try-catch` as shown [here](http://stackoverflow.com/a/10174938/2815219). – Raman Sahasi Jan 25 '17 at 11:23
  • 1
    Possible duplicate of [How to check whether a given string is valid JSON in Java](http://stackoverflow.com/questions/10174898/how-to-check-whether-a-given-string-is-valid-json-in-java) – Autar Jan 25 '17 at 11:24

3 Answers3

3

Use JSONObject's constructor from a String

    try {
        JSONObject o = new JSONObject(yourString);
    } catch (JSONException e) {
        LOGGER.error("No valid json");
    }
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
SCouto
  • 7,808
  • 5
  • 32
  • 49
  • is there any method ? which doesnt use try catch block? – sai Jan 25 '17 at 11:12
  • AFAIK this is the better way, you can build a method containing that block to avoid writing several catch's. Would that be acceptable according to your requirements? – SCouto Jan 25 '17 at 11:14
  • thanks for the reply. i just dont wanna use try catch blocks at all. like in javascript we hava a predefined function which validates string is json or not.. so i thought is there anything in java which automatically checks valid json or not.. i want boolean jsIsValid=method(string); – sai Jan 25 '17 at 11:20
  • 1
    @edsheeran First of all, Java is not JavaScript. Deal with it. And even [in JavaScript you are supposed to use `try-catch`](http://stackoverflow.com/a/9804835/1426227). – cassiomolin Jan 25 '17 at 11:22
0
public boolean isValidJson(String jsonStr) {
    Object json = new JSONTokener(jsonStr)).nextValue();
    if (json instanceof JSONObject || json instanceof JSONArray) {
      return true;
    } else {
      return false;
    }
}

Just pass any string in this function.

Sanjay Kushwah
  • 190
  • 1
  • 9
0
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;    
/**
         * 
         * @param inputJosn
         * @return
         * @throws IOException 
         * @throws JsonParseException 
         * @throws JsonProcessingException
         */
        private static boolean isJsonValid(String inputJosn) throws JsonParseException, IOException  {
            ObjectMapper mapper = new ObjectMapper();
            mapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY);
            JsonFactory factory = mapper.getFactory();
            JsonParser parser = factory.createParser(inputJosn);
            JsonNode jsonObj = mapper.readTree(parser);
            System.out.println(jsonObj.toString());


            return true;
        }
vaquar khan
  • 10,864
  • 5
  • 72
  • 96